Object lookup in Array

JavaScript performance comparison

Test case created by Davit Barbakadze

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var array = [], countDuplicates = 0;
   
    function getRandom(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }
   
    function populateArray() {
        for (var i = 0; i < 1000000; i++) {
            array.push({
                name: "name_" + i,
                size: getRandom(100, 1000)
            });
        }
    }
   
};
</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
Lookup by name
for (var i = 0, length = array.length; i < length; i++) {
        if (array[i].name === "name_9999") {
        countDuplicates++;
    }
}
pending…
Lookup by name and size
for (var i = 0, length = array.length; i < length; i++) {
        if (array[i].name === "name_9999" && size == 800) {
        countDuplicates++;
    }
}
pending…

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

Compare results of other browsers

0 comments

Add a comment