getHash

JavaScript performance comparison

Test case created by George

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var testObject = {theNumber: 42};
   
    var objectId = 1000000;
   
    getHash1 = function(value) {
        if (typeof value === 'number' || typeof value === 'boolean') {
            return value;
        } else if (typeof value === 'string') {
            return '"' + value + '"';
        } else if (value === undefined) {
            return 'undefined';
        } else if (value === null) {
            return 'null';
        } else {
            value._hash = value._hash || ('[object ' + (objectId++) + ']');
            return value._hash;
        }
    }
   
    function getHash2(value) {
        if (typeof value === 'object') {
            if (value === null)
                return 'null';
            value._hash = value._hash || ('[object ' + (objectId++) + ']');
            return value._hash;
        }
        if (typeof value === 'string')
            return "'" + value;
        return '' + value;
    }
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Original getHash()
getHash1()
getHash1(null)
getHash1(true)
getHash1(false)
getHash1(42)
getHash1('42')
 
pending…
Modified getHash()
getHash2()
getHash2(null)
getHash2(true)
getHash2(false)
getHash2(42)
getHash2('42')
 
pending…

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

Compare results of other browsers

0 comments

Add a comment