hash funcs

Benchmark created on


Setup

var hashCode = function(str){
        var hash = 0;
        if (str.length == 0) return hash;
        for (i = 0; i < str.length; i++) {
            char = str.charCodeAt(i);
            hash = ((hash<<5)-hash)+char;
            hash = hash & hash; // Convert to 32bit integer
        }
        return hash;
    }
    
    var djb2Code = function(str){
        var hash = 5381;
        for (i = 0; i < str.length; i++) {
            char = str.charCodeAt(i);
            hash = ((hash << 5) + hash) + char; /* hash * 33 + c */
        }
        return hash;
    }
    
    var sdbmCode = function(str){
        var hash = 0;
        for (i = 0; i < str.length; i++) {
            char = str.charCodeAt(i);
            hash = char + (hash << 6) + (hash << 16) - hash;
        }
        return hash;
    }
    
    var loseCode = function(str){
        var hash = 0;
        for (i = 0; i < str.length; i++) {
            char = str.charCodeAt(i);
            hash += char;
        }
        return hash;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
hashCode
hashCode("abc"+hashCode("hello world"+Math.random()));
ready
djb2Code
djb2Code("abc"+djb2Code("hello world"+Math.random()));
ready
sdbmCode
sdbmCode("abc"+sdbmCode("hello world"+Math.random()));
ready
loseCode
loseCode("abc"+loseCode("hello world"+Math.random()));
ready

Revisions

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