search string in array vs object keys

JavaScript performance comparison

Test case created by shani mahadeva

Info

search for a string in array values vs in object keys

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = ["john", "adams", "serra"];
    var obj = {
      "john": true,
      "adams": true,
      "serra": true
    }
    var f1 = function(str) {
     for(var i=0;i<arr.length;i++)
      if(arr[i]==str) return true;
     return false;
    }
   
    var f2 = function(str) {
      return str in obj;
    }
};
</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
array search
f1("john");
f1("audi");
pending…
object key search
f2("john");
f2("audi");
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