JSPM

pop-hash

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 8929
  • Score
    100M100P100Q125607F
  • License MIT

A hash function for arbitrary objects

Package Exports

  • pop-hash

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pop-hash) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Hash

Hash is a function that will consistently return the same, almost unique value for any given value, particularly objects. Hashing is suitable for grouping objects into buckets with a low probability of multiple non-identical values sharing the same bucket.

var hash = require("pop-hash");
hash([])
hash({})
hash(1)

Polymorphic operator

A well-planned system of objects is beautiful: a system where every meaningful method for an object has been anticipated in the design. Inevitably, another layer of architecture introduces a new concept and with it the temptation to monkey-patch, dunk-punch, or otherwise cover-up the omission. But reaching backward in time, up through the layers of architecture doesn't always compose well, when different levels introduce concepts of the same name but distinct behavior.

A polymorphic operator is a function that accepts as its first argument an object and varies its behavior depending on its type. Such an operator has the benefit of covering for the types from higher layers of architecture, but defers to the eponymous method name of types yet to be defined.

hash({
    hash: function () {
        return JSON.stringify(this);
    }
})

Implementation

The implementation of this object identity hash varies between browsers and Node.js using the "browser" configuration in package.json, which is respected by Browserify and similar tools, particularly Mr.

The implementation uses a WeakMap or a WeakMap shim to assign and recall a randomly generated number to every object it encounters. Hash methods in general may return either strings or numbers, since either is suitable for use as a key in a plain object. Non object values pass through hash.

The Node.js implementation once took advantage of Aleksey Smolenchuck's objhash module, which uses V8's own internal object hash function, but but the binary dependency prooved a burden to maintain.