hashing test popopo

Benchmark created by posinskadpilot on


Preparation HTML

<div id="test-1">Lorem Ipsum</div>
<div id="test-2">Lorem ipsum dolor sit amet</div>
<div id="test-3">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
<div id="test-4">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis sodales vestibulum suscipit. Duis posuere tincidunt dolor id faucibus. Cras ac porttitor sapien. Cras mattis ac lorem at imperdiet. Nullam pharetra erat at luctus lobortis. Phasellus in ante et lacus dictum facilisis. Integer ornare enim id libero pharetra convallis. Phasellus commodo mi eleifend, faucibus lectus vel, euismod arcu. Duis urna nunc, mattis at semper quis, euismod eu arcu. Etiam nec hendrerit neque, ac pharetra justo.</div>

Setup

String.prototype.hashCode = function(){
        var hash = 0;
        if (this.length == 0) return hash;
        for (i = 0; i < this.length; i++) {
                char = this.charCodeAt(i);
                hash = ((hash<<5)-hash)+char;
                hash = hash & hash; // Convert to 32bit integer
        }
        return hash;
    }
    String.prototype.hashCode2 = function(){
        var hash = 0;
        if (this.length == 0) return hash;
    var r = this.replace(/ /g, '');
        for (i = 0; i < r.length; i++) {
                char = r.charCodeAt(i);
                hash = ((hash<<5)-hash)+char;
                hash = hash & hash; // Convert to 32bit integer
        }
        return Math.abs(hash);
    }
    
    hashString = function(s){
        if(!s) return '';
        return Math.abs(s.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0));
    };

Test runner

Ready to run.

Testing in
TestOps/sec
1
document.getElementById('test-2').innerHTML.hashCode()
ready
2
document.getElementById('test-2').innerHTML.hashCode2()
ready
3
hashString(document.getElementById('test-2').innerHTML)
ready

Revisions

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

  • Revision 1: published by posinskadpilot on