object-code
Advanced tools
Comparing version 1.2.4 to 1.3.0
/** | ||
* Hashes a given value into a unique number. | ||
* | ||
* This function accepts **ANY** kind of value, like `functions`, `classes`, `objects` and so on. | ||
* This function accepts **ANY** kind of value, like `functions`, `classes`, `objects` and | ||
* so on. | ||
* | ||
@@ -6,0 +7,0 @@ * **Note**: Symbols uniqueness are not guaranteed, as they are transformed to strings. |
export * from './hash'; | ||
export * from './serialize'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,2 +0,2 @@ | ||
function r(t){var n=typeof t;if(t&&"object"===n&&!(t instanceof Date||t instanceof RegExp)){for(var e=Array.isArray(t)?[]:{},o=Object.keys(t).sort(function(r,t){return r>t?1:-1}),i=o.length;i--;){var a=o[i];e[a]=r(t[a])}return String(t.constructor)+JSON.stringify(e,o)}return n+String(t)}exports.hash=function(t){t=r(t);for(var n=5381,e=0;e<t.length;)n=33*n^t.charCodeAt(e++);return n},exports.serialize=r; | ||
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; | ||
//# 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){var n=typeof e;if(e&&"object"===n&&!(e instanceof Date||e instanceof RegExp)){for(var o=Array.isArray(e)?[]:{},r=Object.keys(e).sort(function(e,t){return e>t?1:-1}),i=r.length;i--;){var f=r[i];o[f]=t(e[f])}return String(e.constructor)+JSON.stringify(o,r)}return n+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(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}); | ||
//# sourceMappingURL=index.umd.js.map |
@@ -18,3 +18,3 @@ /** | ||
*/ | ||
export declare function serialize<T>(value?: T): string; | ||
export declare function serialize<T>(value?: T, seen?: WeakMap<object, any>): string; | ||
//# sourceMappingURL=serialize.d.ts.map |
{ | ||
"name": "object-code", | ||
"version": "1.3.0", | ||
"description": "A blazing fast hash code generator that supports every possible javascript value.", | ||
"version": "1.2.4", | ||
"source": "src/index.ts", | ||
"sideEffects": false, | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"module": "dist/index.mjs", | ||
"umd:main": "dist/index.umd.js", | ||
"unpkg": "dist/index.umd.js", | ||
"author": "Arthur Fiorette <npm@arthur.place>", | ||
"license": "MIT", | ||
"homepage": "https://tinylibs.js.org/packages/object-code", | ||
"bugs": "https://github.com/arthurfiorette/tinylibs/issues", | ||
"homepage": "https://tinylibs.js.org/packages/object-code", | ||
"repository": { | ||
@@ -21,9 +12,11 @@ "type": "git", | ||
}, | ||
"scripts": { | ||
"build": "microbundle --tsconfig tsconfig.build.json", | ||
"test": "jest --coverage" | ||
}, | ||
"devDependencies": { | ||
"microbundle": "0.15.0" | ||
}, | ||
"license": "MIT", | ||
"author": "Arthur Fiorette <npm@arthur.place>", | ||
"sideEffects": false, | ||
"main": "dist/index.js", | ||
"umd:main": "dist/index.umd.js", | ||
"unpkg": "dist/index.umd.js", | ||
"module": "dist/index.mjs", | ||
"source": "src/index.ts", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
@@ -34,3 +27,14 @@ "dist", | ||
"README.md" | ||
] | ||
], | ||
"scripts": { | ||
"build": "microbundle --tsconfig tsconfig.build.json", | ||
"test": "jest --coverage", | ||
"benchmark": "pnpm build && node benchmark/benchmark.js" | ||
}, | ||
"devDependencies": { | ||
"microbundle": "0.15.1", | ||
"@types/object-hash": "2.2.1", | ||
"benny": "3.7.1", | ||
"object-hash": "3.0.0" | ||
} | ||
} |
@@ -105,2 +105,3 @@ <br /> | ||
tags: ['person', 'male', 'programmer'] | ||
recursive: myWeirdObject, | ||
// etc | ||
@@ -133,18 +134,18 @@ }; | ||
Object Code: | ||
98 989 ops/s, ±0.71% | 92.94% slower | ||
103 694 ops/s, ±1.73% | 93.55% slower | ||
Object Hash: | ||
25 649 ops/s, ±0.47% | slowest, 98.17% slower | ||
26 845 ops/s, ±1.99% | slowest, 98.33% slower | ||
Object Code (Jsonified Object): | ||
279 954 ops/s, ±1.35% | 80.04% slower | ||
310 979 ops/s, ±0.76% | 80.67% slower | ||
Object Hash (Jsonified Object): | ||
125 125 ops/s, ±0.43% | 91.08% slower | ||
127 352 ops/s, ±0.76% | 92.08% slower | ||
Object Code (String): | ||
1 402 715 ops/s, ±0.31% | fastest | ||
1 608 518 ops/s, ±1.37% | fastest | ||
Object Hash (String): | ||
250 241 ops/s, ±0.60% | 82.16% slower | ||
239 228 ops/s, ±0.82% | 85.13% slower | ||
``` | ||
@@ -151,0 +152,0 @@ |
@@ -6,3 +6,4 @@ import { serialize } from './serialize'; | ||
* | ||
* This function accepts **ANY** kind of value, like `functions`, `classes`, `objects` and so on. | ||
* This function accepts **ANY** kind of value, like `functions`, `classes`, `objects` and | ||
* so on. | ||
* | ||
@@ -9,0 +10,0 @@ * **Note**: Symbols uniqueness are not guaranteed, as they are transformed to strings. |
@@ -18,3 +18,3 @@ /** | ||
*/ | ||
export function serialize<T>(value?: T): string { | ||
export function serialize<T>(value?: T, seen = new WeakMap()): string { | ||
const type = typeof value; | ||
@@ -31,9 +31,24 @@ | ||
const key = keys[i]! as keyof T; | ||
copy[key] = serialize(value[key]); | ||
const val = value[key]!; | ||
// Uses an internal WeakMap to keep track of previous seen values | ||
// and avoid circular references serializations which would cause | ||
// an infinite loop. | ||
if ( | ||
typeof val === 'object' && | ||
val !== null && | ||
!(val instanceof Date || val instanceof RegExp) | ||
) { | ||
if (seen.has(val)) { | ||
continue; | ||
} | ||
// Only add the value to the seen list if it's an object | ||
seen.set(val, true); | ||
} | ||
copy[key] = serialize(val, seen); | ||
} | ||
return ( | ||
//@ts-expect-error ignore if not present | ||
String(value.constructor) + JSON.stringify(copy, keys) | ||
); | ||
return String(value.constructor) + JSON.stringify(copy, keys); | ||
} | ||
@@ -40,0 +55,0 @@ |
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
27681
143
160
4