Array indexOf vs. Hashtable Lookup

JavaScript performance comparison

Test case created by Paul Young


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


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

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

Benchmark.prototype.setup = function() {
    var primitiveToFind = "X" + Math.floor(Math.random() * 1000)

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);
Hashtable Style
var x = array[primitiveToFind];

Compare results of other browsers


You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:


Comment form temporarily disabled.

Add a comment