sparse arrays vs objects

JavaScript performance comparison

Test case created by Bergi

Preparation code

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