Skip to content

Commit

Permalink
bespoke handling of object types to reduce work
Browse files Browse the repository at this point in the history
  • Loading branch information
planttheidea committed Dec 30, 2022
1 parent e426cc4 commit b24c75d
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 117 deletions.
222 changes: 111 additions & 111 deletions results_latest.txt
Original file line number Diff line number Diff line change
@@ -1,188 +1,188 @@
Benchmark cycles: 1,000 5,000 10,000 50,000 100,000 500,000 1,000,000 5,000,000

Boolean:
1,000: 0.001 sec
5,000: 0.001 sec
1,000: 0 sec
5,000: 0.002 sec
10,000: 0.001 sec
50,000: 0.003 sec
50,000: 0.002 sec
100,000: 0.002 sec
500,000: 0.01 sec
1,000,000: 0.021 sec
5,000,000: 0.106 sec
Average: 45,972,414 ops/sec
500,000: 0.008 sec
1,000,000: 0.016 sec
5,000,000: 0.082 sec
Average: 58,991,150 ops/sec

Infinity:
1,000: 0.001 sec
1,000: 0 sec
5,000: 0.001 sec
10,000: 0.001 sec
50,000: 0.003 sec
100,000: 0.004 sec
500,000: 0.02 sec
1,000,000: 0.039 sec
5,000,000: 0.196 sec
Average: 25,154,717 ops/sec
100,000: 0.003 sec
500,000: 0.016 sec
1,000,000: 0.03 sec
5,000,000: 0.151 sec
Average: 32,517,073 ops/sec

NaN:
1,000: 0 sec
5,000: 0 sec
10,000: 0 sec
10,000: 0.001 sec
50,000: 0.001 sec
100,000: 0.003 sec
500,000: 0.013 sec
1,000,000: 0.027 sec
5,000,000: 0.135 sec
Average: 37,240,223 ops/sec
100,000: 0.002 sec
500,000: 0.01 sec
1,000,000: 0.021 sec
5,000,000: 0.104 sec
Average: 47,956,835 ops/sec

null:
1,000: 0 sec
1,000: 0.001 sec
5,000: 0 sec
10,000: 0 sec
50,000: 0.002 sec
10,000: 0.001 sec
50,000: 0.003 sec
100,000: 0.002 sec
500,000: 0.014 sec
1,000,000: 0.028 sec
5,000,000: 0.139 sec
Average: 36,032,432 ops/sec
500,000: 0.01 sec
1,000,000: 0.021 sec
5,000,000: 0.103 sec
Average: 47,276,596 ops/sec

Number:
1,000: 0 sec
5,000: 0 sec
10,000: 0 sec
50,000: 0.002 sec
50,000: 0.001 sec
100,000: 0.002 sec
500,000: 0.013 sec
1,000,000: 0.026 sec
5,000,000: 0.129 sec
Average: 38,755,814 ops/sec
500,000: 0.01 sec
1,000,000: 0.02 sec
5,000,000: 0.1 sec
Average: 50,120,301 ops/sec

String:
1,000: 0 sec
5,000: 0 sec
5,000: 0.001 sec
10,000: 0.001 sec
50,000: 0.001 sec
100,000: 0.002 sec
50,000: 0.002 sec
100,000: 0.003 sec
500,000: 0.01 sec
1,000,000: 0.021 sec
5,000,000: 0.104 sec
Average: 47,956,835 ops/sec
1,000,000: 0.02 sec
5,000,000: 0.103 sec
Average: 47,614,286 ops/sec

undefined:
1,000: 0 sec
5,000: 0 sec
10,000: 0 sec
50,000: 0.002 sec
10,000: 0.001 sec
50,000: 0.001 sec
100,000: 0.004 sec
500,000: 0.018 sec
1,000,000: 0.037 sec
5,000,000: 0.18 sec
Average: 27,659,751 ops/sec
500,000: 0.016 sec
1,000,000: 0.033 sec
5,000,000: 0.163 sec
Average: 30,577,982 ops/sec

Function:
1,000: 0.001 sec
1,000: 0 sec
5,000: 0.001 sec
10,000: 0.002 sec
50,000: 0.008 sec
100,000: 0.016 sec
500,000: 0.081 sec
1,000,000: 0.158 sec
5,000,000: 0.805 sec
Average: 6,218,284 ops/sec
50,000: 0.007 sec
100,000: 0.015 sec
500,000: 0.073 sec
1,000,000: 0.146 sec
5,000,000: 0.728 sec
Average: 6,858,025 ops/sec

RegExp:
1,000: 0.001 sec
5,000: 0.003 sec
5,000: 0.002 sec
10,000: 0.006 sec
50,000: 0.015 sec
100,000: 0.03 sec
500,000: 0.149 sec
1,000,000: 0.297 sec
5,000,000: 1.488 sec
Average: 3,351,433 ops/sec
500,000: 0.146 sec
1,000,000: 0.294 sec
5,000,000: 1.468 sec
Average: 3,397,554 ops/sec

Array:
1,000: 0.002 sec
5,000: 0.006 sec
10,000: 0.009 sec
50,000: 0.035 sec
100,000: 0.068 sec
500,000: 0.34 sec
1,000,000: 0.673 sec
1,000: 0.003 sec
5,000: 0.005 sec
10,000: 0.012 sec
50,000: 0.036 sec
100,000: 0.074 sec
500,000: 0.367 sec
1,000,000: 0.699 sec
5,000,000: 3.331 sec
Average: 1,493,280 ops/sec
Average: 1,472,498 ops/sec

Date:
1,000: 0 sec
5,000: 0.002 sec
10,000: 0.002 sec
5,000: 0.001 sec
10,000: 0.003 sec
50,000: 0.01 sec
100,000: 0.015 sec
500,000: 0.074 sec
1,000,000: 0.147 sec
5,000,000: 0.735 sec
Average: 6,767,513 ops/sec
100,000: 0.014 sec
500,000: 0.072 sec
1,000,000: 0.144 sec
5,000,000: 0.718 sec
Average: 6,929,314 ops/sec

Error:
1,000: 0.001 sec
5,000: 0.005 sec
10,000: 0.013 sec
50,000: 0.052 sec
100,000: 0.102 sec
500,000: 0.498 sec
1,000,000: 1.017 sec
5,000,000: 4.996 sec
Average: 997,307 ops/sec
50,000: 0.049 sec
100,000: 0.097 sec
500,000: 0.483 sec
1,000,000: 0.966 sec
5,000,000: 4.899 sec
Average: 1,023,491 ops/sec

Event:
1,000: 0.002 sec
5,000: 0.005 sec
10,000: 0.009 sec
50,000: 0.029 sec
5,000: 0.006 sec
10,000: 0.008 sec
50,000: 0.03 sec
100,000: 0.059 sec
500,000: 0.293 sec
1,000,000: 0.586 sec
5,000,000: 2.935 sec
Average: 1,701,378 ops/sec
500,000: 0.291 sec
1,000,000: 0.582 sec
5,000,000: 2.913 sec
Average: 1,713,184 ops/sec

Map:
1,000: 0.002 sec
5,000: 0.007 sec
10,000: 0.007 sec
50,000: 0.031 sec
100,000: 0.061 sec
500,000: 0.308 sec
1,000,000: 0.613 sec
5,000,000: 3.083 sec
Average: 1,621,109 ops/sec
1,000: 0.001 sec
5,000: 0.006 sec
10,000: 0.006 sec
50,000: 0.03 sec
100,000: 0.06 sec
500,000: 0.298 sec
1,000,000: 0.596 sec
5,000,000: 2.983 sec
Average: 1,674,874 ops/sec

Object:
1,000: 0.003 sec
5,000: 0.007 sec
10,000: 0.011 sec
50,000: 0.056 sec
100,000: 0.11 sec
500,000: 0.549 sec
1,000,000: 1.095 sec
5,000,000: 5.478 sec
Average: 912,026 ops/sec
50,000: 0.055 sec
100,000: 0.105 sec
500,000: 0.525 sec
1,000,000: 1.051 sec
5,000,000: 5.246 sec
Average: 951,878 ops/sec

Object (circular):
1,000: 0.001 sec
1,000: 0.002 sec
5,000: 0.007 sec
10,000: 0.011 sec
50,000: 0.053 sec
10,000: 0.01 sec
50,000: 0.052 sec
100,000: 0.1 sec
500,000: 0.504 sec
1,000,000: 1.009 sec
5,000,000: 5.032 sec
Average: 992,407 ops/sec
500,000: 0.497 sec
1,000,000: 0.994 sec
5,000,000: 4.984 sec
Average: 1,003,009 ops/sec

Set:
1,000: 0.001 sec
5,000: 0.006 sec
10,000: 0.006 sec
50,000: 0.028 sec
10,000: 0.007 sec
50,000: 0.027 sec
100,000: 0.056 sec
500,000: 0.279 sec
1,000,000: 0.559 sec
5,000,000: 2.798 sec
Average: 1,785,695 ops/sec
500,000: 0.275 sec
1,000,000: 0.549 sec
5,000,000: 2.749 sec
Average: 1,816,349 ops/sec
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ export type RecursiveClass = keyof typeof RECURSIVE_CLASSES;
export const HASHABLE_TYPES = {
bigint: 'i',
boolean: 'b',
empty: 'e',
function: 'g',
number: 'n',
object: 'o',
string: 's',
symbol: 's',
undefined: 'u',
} as const;

export type HashableType = keyof typeof HASHABLE_TYPES;
19 changes: 14 additions & 5 deletions src/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,25 @@ export function stringify(
): string {
const type = typeof value;

if (type === 'object' && value) {
if (value == null || type === 'undefined') {
return HASHABLE_TYPES.empty + value;
}

if (type === 'object') {
return stringifyComplexType(
value,
toString.call(value) as unknown as Class,
state || { cache: new WeakMap(), id: 1 },
);
}

return (
HASHABLE_TYPES[type] +
(type === 'function' || type === 'symbol' ? value.toString() : value)
);
if (type === 'function' || type === 'symbol') {
return HASHABLE_TYPES[type] + value.toString();
}

if (type === 'boolean') {
return HASHABLE_TYPES.boolean + +value;
}

return HASHABLE_TYPES[type] + value;
}

0 comments on commit b24c75d

Please sign in to comment.