sparse arrays vs objects

Benchmark created by Bergi on


Setup

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();

Test runner

Ready to run.

Testing in
TestOps/sec
object set
obj = {}
for (var i=0; i<500; i++)
    obj[~~(Math.random()*1000)] = Math.random();
ready
array set
arr = [];
for (var i=0; i<500; i++)
    arr[~~(Math.random()*1000)] = Math.random();
ready
object get
var result;
for (var prop in obj) {
    result = obj[prop];
    result = obj[~~(Math.random()*1000)];
}
ready
array get
var result;
for (var prop in arr) {
    result = arr[prop];
    result = arr[~~(Math.random()*1000)];
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.