Fastest way to find an item in a list

JavaScript performance comparison

Test case created

Info

Confirming whether it's faster to compare two strings or two objects. This assumes every object in the list has an 'id' value with a unique property, and that the search is looking for a SPECIFIC object regardless of any of that object's other properties. (i.e. display objects in a displaylist)

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var listOfThings = [];
    for(var i=0; i<1000; i++) listOfThings.push({id:"q"+i});
};
</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
Compare by string
var searchingForObject = listOfThings[Math.floor(Math.random()*listOfThings.length)];
for(var i=0; i<listOfThings.length; i++) if (listOfThings[i].id == searchingForObject.id) break;
pending…
Compare by object reference
var searchingForObject = listOfThings[Math.floor(Math.random()*listOfThings.length)];
for(var i=0; i<listOfThings.length; i++) if (listOfThings[i] == searchingForObject) break;
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