Key Or Array search

JavaScript performance comparison

Revision 2 of this test case created by vincent piel


The purpose of this test is to test fastest way to check wether a string belongs to a set of strings.

Preparation code

Benchmark.prototype.setup = function() {
    var i=0;
    var allStrings = ['apple', 'orange', 'tomato', 'lemon', 'banana'];
    var stringObj = {} ;
    for (var i=0; i<allStrings.length; i++) stringObj[allStrings[i]]=true;
    var testStrings = ['orange', 'car', 'lemon', 'banana', 'x', 'lemon', 'void'];
    var res=false;

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
array search
for (i=0; i<testStrings.length; i++) { 
   res = allStrings.indexOf(testStrings[i])!=-1 ;
in operator
for (i=0; i<testStrings.length; i++) { 
   res = testStrings[i] in stringObj;
object property check
for (i=0; i<testStrings.length; i++) { 
   res =  stringObj[testStrings[i]] || false;

Compare results of other browsers


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:


Comment form temporarily disabled.

Add a comment