Array Filter vs Simple Loop

JavaScript performance comparison

Revision 2 of this test case created by Md Khan

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var myArr = [{"number":1,"name":"Fred"},{"number":2,"name":"John"},{"number":3,"name":"Toni"},{"number":4,"name":"Sarah"},{"number":5,"name":"Gill"}];
  
  function byForLoop(myArr, num){
   var i, l;
    for (i = 0, l = myArr.length; i < l; i += 1) {
      if (myArr[i].number === num) {
          return [myArr[i]];
      }
   }
   return [];
  }
  
  function byFilter(myArr, num){
  return myArr.filter(function (el) {
      return el.number === num;
  });
  }

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Array Filter
byFilter(myArr, 3);
pending…
Simple Loop
byForLoop(myArr, 3);
pending…

Compare results of other browsers

Revisions

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

0 Comments