sparse arrays vs objects

JavaScript performance comparison

Revision 3 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    window.obj = {};
    for (var i = 0; i < 25; i++)
    obj[i] = Math.random();
    window.arr = [];
    for (var i = 0; i < 25; i++)
    arr[i] = Math.random();
};
</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
not relevant
obj = {}
for (var i = 0; i < 1; i++)
obj[~~ (Math.random() * 1000)] = Math.random();
pending…
not relevant
arr = [];
for (var i = 0; i < 1; i++)
arr[~~ (Math.random() * 1000)] = Math.random();
pending…
object get
var result;
for (var i = 0; i < 25; i++)
result = obj[i];
pending…
array get
var result;
for (var i = 0; i < 25; i++)
result = arr[i];
pending…

Compare results of other browsers

Revisions

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:

0 comments

Add a comment