Array indexOf vs. Hashtable Lookup

JavaScript performance comparison

Test case created by Paul Young

Info

A test to see if there are any performance gains during lookup between adding primitives to an array, and structuring an array of primitives as a hash table.

Preparation code

<script>

var array = [];
for (var i = 0; ++i < 10000;)
        array.push("X" + i);

var hashtable = {};
for (var i = 0; ++i < 10000;)
        hashtable["X" + i] = true;

</script>
<script>
Benchmark.prototype.setup = function() {
    var primitiveToFind = "X" + Math.floor(Math.random() * 1000)
};
</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
Array Style
var x = array.indexOf(primitiveToFind);
pending…
Hashtable Style
var x = array[primitiveToFind];
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