hash for vs reduce

Benchmark created on


Preparation HTML

var hashCodeFor = function(s){
  return s.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0);              
};

var hashCodeReduce = function(s){
    var hash = 0, i, char;
    if (s.length == 0) return hash;
    for (i = 0, l = s.length; i < l; i++) {
        char  = s.charCodeAt(i);
        hash  = ((hash<<5)-hash)+char;
        hash |= 0; // Convert to 32bit integer
    }
    return hash;
};

Setup

var hashCodeFor = function(s){
      return s.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0);              
    };
    
    var hashCodeReduce = function(s){
        var hash = 0, i, char;
        if (s.length == 0) return hash;
        for (i = 0, l = s.length; i < l; i++) {
            char  = s.charCodeAt(i);
            hash  = ((hash<<5)-hash)+char;
            hash |= 0; // Convert to 32bit integer
        }
        return hash;
    };

Test runner

Ready to run.

Testing in
TestOps/sec
for
hashCodeFor('{"width":1463,"height":450,"background":{"width":1463,"height":918,"x":0,"y":0,"saturation":0.5,"url":"http://simongrondin.name/files/map-reporting/map2background.jpg","base64":""},"dashboard":{"x":10,"y":10,"width":480,"height":150},"scale":{"initial":"blue","alternate":"green","width":800,"height":10,"x":500,"y":10,"showNumbers":true},"threshold":70,"showIDs":false,"labels":{"value":"Satisfaction: ","link":"View data"}}');
ready
Reduce
hashCodeReduce('{"width":1463,"height":450,"background":{"width":1463,"height":918,"x":0,"y":0,"saturation":0.5,"url":"http://simongrondin.name/files/map-reporting/map2background.jpg","base64":""},"dashboard":{"x":10,"y":10,"width":480,"height":150},"scale":{"initial":"blue","alternate":"green","width":800,"height":10,"x":500,"y":10,"showNumbers":true},"threshold":70,"showIDs":false,"labels":{"value":"Satisfaction: ","link":"View data"}}');
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.