sparse arrays vs objects

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

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