Skip to content

Instantly share code, notes, and snippets.

@xemasiv
Last active April 12, 2018 16:12
Show Gist options
  • Save xemasiv/edf38f725915279a8536c050973bd4a5 to your computer and use it in GitHub Desktop.
Save xemasiv/edf38f725915279a8536c050973bd4a5 to your computer and use it in GitHub Desktop.
Object Signatures

Summary

For creating signatures of objects / class instances.

Takes Object, returns String

  • Sample use case?
    • Memoization, for caching results of expensive functions.
    • This function generates a key that you can use for caching.
  • Why CircularJSON instead of JSON.stringify?
    • To keep us error-free on objects with circular references.
  • Why sha256?
    • It's better than md5 / sha1 (that's what she says)
const hasha = require('hasha');
const CircularJSON = require('circular-json');
let signature = hasha(
CircularJSON.stringify(query),
{ algorithm: 'sha256' }
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment