class Foo {}hash(Foo) === hash(Foo)hash(Foo) !== hash(class {})
const foo = new Set([1])hash(foo) === hash(foo)hash(foo) !== hash(new Set([1]))
Notes
This function does something similar to JSON.stringify, but more than it. It doesn't generate a secure checksum, which usually has a fixed length and is hard to be reversed. With stable-hash it's still possible to get the original data. Also, the output might include any charaters, not just alphabets and numbers like other hash algorithms. So:
Use another encoding layer on top of it if you want to display the output.
Use another crypto layer on top of it if you want to have a secure and fixed length hash.
import crypto from 'crypto'import hash from 'stable-hash'const weakHash = hash(anyJavaScriptValueHere)const encodedHash = Buffer.from(weakHash).toString('base64')const safeHash = crypto.createHash('MD5').update(weakHash).digest('hex')
Also, the consistency of this lib is sometimes guaranteed by the singularity of the WeakMap instance. So it might not generate the consistent results when running in different runtimes, e.g. server/client or parent/worker scenarios.
License
Created by Shu Ding. Released under the MIT License.