indexOf vs hash

JavaScript performance comparison

Test case created by Anton Ignatov

Preparation code

<script>
var arr = [],
    hash = {};

for (var i = 0; arr.length < 1000; i++) {
    var t = Math.floor(Math.random() * 5000);

    if (!hash[t]) {
        hash[t] = 1;
        arr.push(1);
    }
}
console.log(arr, hash);
</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
indexOf
var a = Math.floor(Math.random() * 5000),
    x = arr.indexOf(a);
pending…
hash
var b = Math.floor(Math.random() * 5000),
    y = hash[b];
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