Skip to content

Instantly share code, notes, and snippets.

@gpoole
Created August 28, 2017 00:26
Show Gist options
  • Save gpoole/98b52ce1962bf5fca69fe6af5b092997 to your computer and use it in GitHub Desktop.
Save gpoole/98b52ce1962bf5fca69fe6af5b092997 to your computer and use it in GitHub Desktop.

Hashing in node

  • Farmhash is really fast
  • sha1 is the fastest hash in crypto

Benchmarks

sha1 (crypto) x 222,654 ops/sec ±2.06% (79 runs sampled)
md5 (crypto) x 196,396 ops/sec ±1.82% (78 runs sampled)
sha256 (crypto) x 155,714 ops/sec ±1.52% (82 runs sampled)
sha512 (crypto) x 168,394 ops/sec ±1.49% (82 runs sampled)
hash32 (farmhash) x 610,034 ops/sec ±1.32% (87 runs sampled)
hash64 (farmhash) x 470,859 ops/sec ±0.96% (83 runs sampled)

Object hashing libraries

  • hash-object has its own internal serialisation algorithm and sorts as it goes, crypto to generate the hash
  • object-hash seems to stream the object into the crypto hashing functions, sorts object keys as they come up
  • node-object-hash just sorts, JSON.stringifys and hashes
  • hash-obj just sorts, JSON.stringifys and hashes
  • Custom sort + stringify with farmhash 64-bit hash

Benchmarks

Note that all the libraries had their algorithm set to sha1, some default to different ones

object-hash x 451 ops/sec ±1.31% (77 runs sampled)
node-object-hash x 9,097 ops/sec ±3.09% (79 runs sampled)
hash-obj x 7,950 ops/sec ±1.35% (85 runs sampled)
hash-object x 14,907 ops/sec ±1.98% (81 runs sampled)
sortKeys | JSON.stringify | farmhash x 6,322 ops/sec ±1.45% (82 runs sampled)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment