Skip to content

Instantly share code, notes, and snippets.

@heymichaelp
Created March 13, 2014 14:40
Show Gist options
  • Save heymichaelp/9529741 to your computer and use it in GitHub Desktop.
Save heymichaelp/9529741 to your computer and use it in GitHub Desktop.
res for Objects.","main":"sigmund.js","directories":{"test":"test"},"dependencies":{},"devDependencies":{"tap":"~0.3.0"},"scripts":{"test":"tap test/*.js","bench":"node bench.js"},"repository":{"type":"git","url":"git://github.com/isaacs/sigmund"},"keywords":["object","signature","key","data","psychoanalysis"],"author":{"name":"Isaac Z. Schlueter","email":"i@izs.me","url":"http://blog.izs.me/"},"license":"BSD","readme":"# sigmund\u000a\u000aQuick and dirty signatures for Objects.\u000a\u000aThis is like a much faster `deepEquals` comparison, which returns a\u000astring key suitable for caches and the like.\u000a\u000a## Usage\u000a\u000a```javascript\u000afunction doSomething (someObj) {\u000a var key = sigmund(someObj, maxDepth) // max depth defaults to 10\u000a var cached = cache.get(key)\u000a if (cached) return cached)\u000a\u000a var result = expensiveCalculation(someObj)\u000a cache.set(key, result)\u000a return result\u000a}\u000a```\u000a\u000aThe resulting key will be as unique and reproducible as calling\u000a`JSON.stringify` or `util.inspect` on the object, but is much faster.\u000aIn order to achieve this speed, some differences are glossed over.\u000aFor example, the object `{0:'foo'}` will be treated identically to the\u000aarray `['foo']`.\u000a\u000aAlso, just as there is no way to summon the soul from the scribblings\u000aof a cocain-addled psychoanalyst, there is no way to revive the object\u000afrom the signature string that sigmund gives you. In fact, it's\u000abarely even readable.\u000a\u000aAs with `sys.inspect` and `JSON.stringify`, larger objects will\u000aproduce larger signature strings.\u000a\u000aBecause sigmund is a bit less strict than the more thorough\u000aalternatives, the strings will be shorter, and also there is a\u000aslightly higher chance for collisions. For example, these objects\u000ahave the same signature:\u000a\u000a var obj1 = {a:'b',c:/def/,g:['h','i',{j:'',k:'l'}]}\u000a var obj2 = {a:'b',c:'/def/',g:['h','i','{jkl']}\u000a\u000aLike a good Freudian, sigmund is most effective when you already have\u000asome understanding of what you're looking for. It can help you help\u000ayourself, but you must be willing to do some work as well.\u000a\u000aCycles are handled, and cyclical objects are silently omitted (though\u000athe key is included in the signature output.)\u000a\u000aThe second argument is the maximum depth, which defaults to 10,\u000abecause that is the maximum object traversal depth covered by most\u000ainsurance carriers.\u000a","_id":"sigmund@1.0.0","dist":{"shasum":"66a2b3a749ae8b5fb89efd4fcc01dc94fbe02296","tarball":"http://registry.npmjs.org/sigmund/-/sigmund-1.0.0.tgz"},"_npmVersion":"1.1.48","_npmUser":{"name":"isaacs","email":"i@izs.me"},"maintainers":[{"name":"isaacs","email":"i@izs.me"}]}},"readme":"# sigmund\u000a\u000aQuick and dirty signatures for Objects.\u000a\u000aThis is like a much faster `deepEquals` comparison, which returns a\u000astring key suitable for caches and the like.\u000a\u000a## Usage\u000a\u000a```javascript\u000afunction doSomething (someObj) {\u000a var key = sigmund(someObj, maxDepth) // max depth defaults to 10\u000a var cached = cache.get(key)\u000a if (cached) return cached)\u000a\u000a var result = expensiveCalculation(someObj)\u000a cache.set(key, result)\u000a return result\u000a}\u000a```\u000a\u000aThe resulting key will be as unique and reproducible as calling\u000a`JSON.stringify` or `util.inspect` on the object, but is much faster.\u000aIn order to achieve this speed, some differences are glossed over.\u000aFor example, the object `{0:'foo'}` will be treated identically to the\u000aarray `['foo']`.\u000a\u000aAlso, just as there is no way to summon the soul from the scribblings\u000aof a cocain-addled psychoanalyst, there is no way to revive the object\u000afrom the signature string that sigmund gives you. In fact, it's\u000abarely even readable.\u000a\u000aAs with `sys.inspect` and `JSON.stringify`, larger objects will\u000aproduce larger signature strings.\u000a\u000aBecause sigmund is a bit less strict than the more thorough\u000aalternatives, the strings will be shorter, and also there is a\u000aslightly higher chance for collisions. For example, these objects\u000ahave the same signature:\u000a\u000a var obj1 = {a:'b',c:/def/,g:['h','i',{j:'',k:'l'}]}\u000a var obj2 = {a:'b',c:'/def/',g:['h','i','{jkl']}\u000a\u000aLike a good Freudian, sigmund is most effective when you already have\u000asome understanding of what you're looking for. It can help you help\u000ayourself, but you must be willing to do some work as well.\u000a\u000aCycles are handled, and cyclical objects are silently omitted (though\u000athe key is included in the signature output.)\u000a\u000aThe second argument is the maximum depth, which defaults to 10,\u000abecause that is the maximum object traversal depth covered by most\u000ainsurance carriers.\u000a","maintainers":[{"name":"isaacs","email":"i@izs.me"}],"time":{"modified":"2012-09-12T16:03:45.412Z","created":"2012-08-13T15:59:10.991Z","1.0.0":"2012-08-13T15:59:12.395Z"},"author":{"name":"Isaac Z. Schlueter","email":"i@izs.me","url":"http://blog.izs.me/"},"repository":{"type":"git","url":"git://github.com/isaacs/sigmund"},"users":{},"_attachments":{}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment