object-code
Advanced tools
Comparing version 1.3.0 to 1.3.1
@@ -25,3 +25,3 @@ /** | ||
*/ | ||
export declare function hash(val?: unknown): number; | ||
export declare function hash(val: unknown, seen?: WeakSet<WeakKey>): number; | ||
//# sourceMappingURL=hash.d.ts.map |
export * from './hash'; | ||
export * from './serialize'; | ||
export * from './util'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,2 +0,2 @@ | ||
function t(e,n){void 0===n&&(n=new WeakMap);var r=typeof e;if(e&&"object"===r&&!(e instanceof Date||e instanceof RegExp)){for(var o=Array.isArray(e)?[]:{},i=Object.keys(e).sort(function(t,e){return t>e?1:-1}),a=i.length;a--;){var f=i[a],s=e[f];if("object"==typeof s&&null!==s&&!(s instanceof Date||s instanceof RegExp)){if(n.has(s))continue;n.set(s,!0)}o[f]=t(s,n)}return String(e.constructor)+JSON.stringify(o,i)}return r+String(e)}exports.hash=function(e){e=t(e);for(var n=5381,r=0;r<e.length;)n=33*n^e.charCodeAt(r++);return n},exports.serialize=t; | ||
function t(t,e){return t>e?1:-1}exports.hash=function e(r,o){var n=5381;if("object"==typeof r&&null!==r&&(r.toString===Object.prototype.toString||r.toString===Array.prototype.toString)){o||(o=new WeakSet);for(var a=Object.keys(r).sort(t),i=0;i<a.length;i++){var c=a[i],f=r[c];if(n=33*n^e(c,o),"object"==typeof f&&null!==f&&!(f instanceof Date||f instanceof RegExp)){if(o.has(f))continue;o.add(f)}n=33*n^e(f,o)}return 33*n^e(r.constructor,o)}for(var s=typeof r+(r instanceof Date?r.getTime():String(r)),p=0;p<s.length;p++)n=33*n^s.charCodeAt(p);return n},exports.sortNumbers=t; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e||self).objectCode={})}(this,function(e){function t(e,n){void 0===n&&(n=new WeakMap);var o=typeof e;if(e&&"object"===o&&!(e instanceof Date||e instanceof RegExp)){for(var i=Array.isArray(e)?[]:{},r=Object.keys(e).sort(function(e,t){return e>t?1:-1}),f=r.length;f--;){var a=r[f],s=e[a];if("object"==typeof s&&null!==s&&!(s instanceof Date||s instanceof RegExp)){if(n.has(s))continue;n.set(s,!0)}i[a]=t(s,n)}return String(e.constructor)+JSON.stringify(i,r)}return o+String(e)}e.hash=function(e){e=t(e);for(var n=5381,o=0;o<e.length;)n=33*n^e.charCodeAt(o++);return n},e.serialize=t}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t||self).objectCode={})}(this,function(t){function e(t,e){return t>e?1:-1}t.hash=function t(o,n){var r=5381;if("object"==typeof o&&null!==o&&(o.toString===Object.prototype.toString||o.toString===Array.prototype.toString)){n||(n=new WeakSet);for(var f=Object.keys(o).sort(e),i=0;i<f.length;i++){var a=f[i],c=o[a];if(r=33*r^t(a,n),"object"==typeof c&&null!==c&&!(c instanceof Date||c instanceof RegExp)){if(n.has(c))continue;n.add(c)}r=33*r^t(c,n)}return 33*r^t(o.constructor,n)}for(var s=typeof o+(o instanceof Date?o.getTime():String(o)),u=0;u<s.length;u++)r=33*r^s.charCodeAt(u);return r},t.sortNumbers=e}); | ||
//# sourceMappingURL=index.umd.js.map |
{ | ||
"name": "object-code", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"description": "A blazing fast hash code generator that supports every possible javascript value.", | ||
@@ -28,12 +28,12 @@ "homepage": "https://tinylibs.js.org/packages/object-code", | ||
"scripts": { | ||
"benchmark": "pnpm build && node benchmark/benchmark.js", | ||
"build": "microbundle --tsconfig tsconfig.build.json", | ||
"test": "jest --coverage", | ||
"benchmark": "pnpm build && node benchmark/benchmark.js" | ||
"test": "jest --coverage" | ||
}, | ||
"devDependencies": { | ||
"@types/object-hash": "3.0.6", | ||
"benny": "3.7.1", | ||
"microbundle": "0.15.1", | ||
"@types/object-hash": "2.2.1", | ||
"benny": "3.7.1", | ||
"object-hash": "3.0.0" | ||
} | ||
} |
@@ -133,18 +133,18 @@ <br /> | ||
Object Code: | ||
103 694 ops/s, ±1.73% | 93.55% slower | ||
287 970 ops/s, ±0.33% | 91.35% slower | ||
Object Hash: | ||
26 845 ops/s, ±1.99% | slowest, 98.33% slower | ||
52 045 ops/s, ±0.30% | slowest, 98.44% slower | ||
Object Code (Jsonified Object): | ||
310 979 ops/s, ±0.76% | 80.67% slower | ||
406 813 ops/s, ±0.23% | 87.78% slower | ||
Object Hash (Jsonified Object): | ||
127 352 ops/s, ±0.76% | 92.08% slower | ||
221 757 ops/s, ±0.30% | 93.34% slower | ||
Object Code (String): | ||
1 608 518 ops/s, ±1.37% | fastest | ||
3 328 770 ops/s, ±0.21% | fastest | ||
Object Hash (String): | ||
239 228 ops/s, ±0.82% | 85.13% slower | ||
456 392 ops/s, ±0.43% | 86.29% slower | ||
``` | ||
@@ -151,0 +151,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { serialize } from './serialize'; | ||
import { sortNumbers } from './util'; | ||
@@ -27,13 +27,57 @@ /** | ||
*/ | ||
export function hash(val?: unknown): number { | ||
val = serialize(val); | ||
export function hash(val: unknown, seen?: WeakSet<WeakKey>): number { | ||
let h = 5381; | ||
let hash = 5381; | ||
let index = 0; | ||
// Objects should be recursively hashed | ||
if ( | ||
typeof val === 'object' && | ||
val !== null && | ||
(val.toString === Object.prototype.toString || | ||
val.toString === Array.prototype.toString) | ||
) { | ||
if (!seen) { | ||
seen = new WeakSet(); | ||
} | ||
while (index < (val as string).length) { | ||
hash = (hash * 33) ^ (val as string).charCodeAt(index++); | ||
// Sort keys to keep the hash consistent | ||
const keys = Object.keys(val).sort(sortNumbers); | ||
for (let i = 0; i < keys.length; i++) { | ||
const key = keys[i]; | ||
const value = val[key as keyof typeof val] as WeakKey; | ||
h = (h * 33) ^ hash(key, seen); | ||
// Uses an internal WeakMap to keep track of previous seen values | ||
// and avoid circular references serializations which would cause | ||
// an infinite loop. | ||
if ( | ||
typeof value === 'object' && | ||
value !== null && | ||
!(value instanceof Date || value instanceof RegExp) | ||
) { | ||
if (seen.has(value)) { | ||
continue; | ||
} | ||
seen.add(value); | ||
} | ||
// Hashes the value | ||
h = (h * 33) ^ hash(value, seen); | ||
} | ||
// Also hashes the constructor | ||
h = (h * 33) ^ hash(val.constructor, seen); | ||
return h; | ||
} | ||
return hash; | ||
const toHash = typeof val + (val instanceof Date ? val.getTime() : String(val)); | ||
for (let i = 0; i < toHash.length; i++) { | ||
h = (h * 33) ^ toHash.charCodeAt(i); | ||
} | ||
return h; | ||
} |
export * from './hash'; | ||
export * from './serialize'; | ||
export * from './util'; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25714
118