4 ms·
Hash codes aren't actually exposed in JavaScript. It's an implementation detail. The only requirement is that key lookup is consistent with equals [1]. For a s
by skybrian 9y ago
Hash codes aren't actually exposed in JavaScript. It's an implementation detail. The only requirement is that key lookup is consistent with equals [1].
For a string key, the hashcode field is probably a cached hash of the value, since that's how an equality comparison works. Most objects are only equal to themselves, are mutable, and get moved in memory, so storing a unique identifier is the only way to know if it's the same object that you saw before.
For a hash code, the identifier doesn't need to be unique since all that matters is that it's consistently placed in the same bucket. All you need is to store a sufficiently long random number.
[1] SameValueZero equality as defined at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equa...
- SloopJon 9y agoUpvoted for pointing out that this random number isn't necessarily unique. There can be collisions, just as with a conventional hash.