Home » Javascript » Is there any kind of hash code function in JavaScript?

Is there any kind of hash code function in JavaScript?

Posted by: admin November 29, 2017 Leave a comment


Basically, I’m trying to create an object of unique objects, a set. I had the brilliant idea of just using a JavaScript object with objects for the property names. Such as,

set[obj] = true;

This works, up to a point. It works great with string and numbers, but with other objects, they all seem to “hash” to the same value and access the same property. Is there some kind of way I can generate a unique hash value for an object? How do strings and numbers do it, can I override the same behavior?


JavaScript objects can only use strings as keys (anything else is converted to a string).

You could, alternatively, maintain an array which indexes the objects in question, and use its index string as a reference to the object. Something like this:

var ObjectReference = [];

set['ObjectReference.' + ObjectReference.indexOf(obj)] = true;

Obviously it’s a little verbose, but you could write a couple of methods that handle it and get and set all willy nilly.


Your guess is fact — this is defined behaviour in JavaScript — specifically a toString conversion occurs meaning that you can can define your own toString function on the object that will be used as the property name. – olliej

This brings up another interesting point; you can define a toString method on the objects you want to hash, and that can form their hash identifier.


If you want a hashCode() function like Java’s in JavaScript, that is yours:

String.prototype.hashCode = function(){
    var hash = 0;
    for (var i = 0; i < this.length; i++) {
        var character = this.charCodeAt(i);
        hash = ((hash<<5)-hash)+character;
        hash = hash & hash; // Convert to 32bit integer
    return hash;

That is the way of implementation in Java (bitwise operator).


The easiest way to do this is to give each of your objects its own unique toString method:

(function() {
    var id = 0;

    /*global MyObject */
    MyObject = function() {
        this.objectId = '<#MyObject:' + (id++) + '>';
        this.toString= function() {
            return this.objectId;

I had the same problem and this solved it perfectly for me with minimal fuss, and was a lot easier that re-implementing some fatty Java style Hashtable and adding equals() and hashCode() to your object classes. Just make sure that you don’t also stick a string ‘<#MyObject:12> into your hash or it will wipe out the entry for your exiting object with that id.

Now all my hashes are totally chill. I also just posted a blog entry a few days ago about this exact topic.


The solution I chose is similar to Daniel’s, but rather than use an object factory and override the toString, I explicitly add the hash to the object when it is first requested through a getHashCode function. A little messy, but better for my needs 🙂

Function.prototype.getHashCode = (function(id) {
    return function() {
        if (!this.hashCode) {
            this.hashCode = '<hash|#' + (id++) + '>';
        return this.hashCode;


What you described is covered by Harmony WeakMaps, part of the ECMAScript 6 specification (next version of JavaScript). That is: a set where the keys can be anything (including undefined) and is non-enumerable.

This means it’s impossible to get a reference to a value unless you have a direct reference to the key (any object!) that links to it. It’s important for a bunch of engine implementation reasons relating to efficiency and garbage collection, but it’s also super cool for in that it allows for new semantics like revokable access permissions and passing data without exposing the data sender.

From MDN:

var wm1 = new WeakMap(),
    wm2 = new WeakMap();
var o1 = {},
    o2 = function(){},
    o3 = window;

wm1.set(o1, 37);
wm1.set(o2, "azerty");
wm2.set(o1, o2); // A value can be anything, including an object or a function.
wm2.set(o3, undefined);
wm2.set(wm1, wm2); // Keys and values can be any objects. Even WeakMaps!

wm1.get(o2); // "azerty"
wm2.get(o2); // Undefined, because there is no value for o2 on wm2.
wm2.get(o3); // Undefined, because that is the set value.

wm1.has(o2); // True
wm2.has(o2); // False
wm2.has(o3); // True (even if the value itself is 'undefined').

wm1.has(o1);   // True
wm1.has(o1);   // False

WeakMaps are available in current Firefox, Chrome and Edge. They’re also supported in Node v7 , and in v6 with the --harmony-weak-maps flag.


I put together a small JavaScript module a while ago to produce hashcodes for strings, objects, arrays, etc. (I just committed it to GitHub 🙂 )


// -2559914341
Hashcode.value({ 'site' : "stackoverflow" })
// -3579752159


For my specific situation I only care about the equality of the object as far as keys and primitive values go. The solution that worked for me was converting the object to its JSON representation and using that as the hash. There are limitations such as order of key definition potentially being inconsistent; but like I said it worked for me because these objects were all being generated in one place.

var hashtable = {};

var myObject = {a:0,b:1,c:2};

var hash = JSON.stringify(myObject);
// '{"a":0,"b":1,"c":2}'

hashtable[hash] = myObject;
// {
//   '{"a":0,"b":1,"c":2}': myObject
// }


The JavaScript specification defines indexed property access as performing a toString conversion on the index name. For example,

myObject[myProperty] = ...;

is the same as

myObject[myProperty.toString()] = ...;

This is necessary as in JavaScript


is the same as


And yes, it makes me sad as well 🙁


In ECMAScript 6 there’s now a Set that works how you’d like: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set

It’s already available in the latest Chrome, FF, and IE11.


Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol

you can use Es6 symbol to create unique key and access object.
Every symbol value returned from Symbol() is unique. A symbol value may be used as an identifier for object properties; this is the data type’s only purpose.

var obj = {};

obj[Symbol('a')] = 'a';
obj[Symbol.for('b')] = 'b';
obj['c'] = 'c';
obj.d = 'd';


Here’s my simple solution that returns a unique integer.

function hashcode(obj) {
    var hc = 0;
    var chars = JSON.stringify(obj).replace(/\{|\"|\}|\:|,/g, '');
    var len = chars.length;
    for (var i = 0; i < len; i++) {
        // Bump 7 to larger prime number to increase uniqueness
        hc += (chars.charCodeAt(i) * 7);
    return hc;


My solution introduces a static function for the global Object object.

(function() {
    var lastStorageId = 0;

    this.Object.hash = function(object) {
        var hash = object.__id;

        if (!hash)
             hash = object.__id = lastStorageId++;

        return '#' + hash;

I think this is more convenient with other object manipulating functions in JavaScript.


If you truly want set behavior (I’m going by Java knowledge), then you will be hard pressed to find a solution in JavaScript. Most developers will recommend a unique key to represent each object, but this is unlike set, in that you can get two identical objects each with a unique key. The Java API does the work of checking for duplicate values by comparing hash code values, not keys, and since there is no hash code value representation of objects in JavaScript, it becomes almost impossible to do the same. Even the Prototype JS library admits this shortcoming, when it says:

“Hash can be thought of as an
associative array, binding unique keys
to values (which are not necessarily



In addition to eyelidlessness’s answer, here is a function that returns a reproducible, unique ID for any object:

var uniqueIdList = [];
function getConstantUniqueIdFor(element) {
    // HACK, using a list results in O(n), but how do we hash e.g. a DOM node?
    if (uniqueIdList.indexOf(element) < 0) {
    return uniqueIdList.indexOf(element);

As you can see it uses a list for look-up which is very inefficient, however that’s the best I could find for now.


If you want to use objects as keys you need to overwrite their toString Method, as some already mentioned here. The hash functions that were used are all fine, but they only work for the same objects not for equal objects.

I’ve written a small library that creates hashes from objects, which you can easily use for this purpose. The objects can even have a different order, the hashes will be the same. Internally you can use different types for your hash (djb2, md5, sha1, sha256, sha512, ripemd160).

Here is a small example from the documentation:

var hash = require('es-hash');

// Save data in an object with an object as a key
Object.prototype.toString = function () {
    return '[object Object #'+hash(this)+']';

var foo = {};

foo[{bar: 'foo'}] = 'foo';

 * Output:
 *  foo
 *  undefined
console.log(foo[{bar: 'foo'}]);

The package can be used either in browser and in Node-Js.

Repository: https://bitbucket.org/tehrengruber/es-js-hash


If you want to have unique values in a lookup object you can do something like this:

Creating a lookup object

var lookup = {};

Setting up the hashcode function

function getHashCode(obj) {
    var hashCode = '';
    if (typeof obj !== 'object')
        return hashCode + obj;
    for (var prop in obj) // No hasOwnProperty needed
        hashCode += prop + getHashCode(obj[prop]); // Add key + value to the result string
    return hashCode;


var key = getHashCode({ 1: 3, 3: 7 });
// key = '1337'
lookup[key] = true;


var key = getHashCode([1, 3, 3, 7]);
// key = '01132337'
lookup[key] = true;

Other types

var key = getHashCode('StackOverflow');
// key = 'StackOverflow'
lookup[key] = true;

Final result

{ 1337: true, 01132337: true, StackOverflow: true }

Do note that getHashCode doesn’t return any value when the object or array is empty

// '012'
// '012'

This is similar to @ijmacd solution only getHashCode doesn’t has the JSON dependency.