Scotch array filter

JavaScript performance comparison

Test case created by worldclassdev

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  // AN IMPERATIVE APPROACH
  
  function arrayFilterImperative(arr, func) {
    for (let elem of arr) {
      if (func(elem)) {
        return elem;
      }
    }
    return undefined;
  }
  
  // .FILTER
  
  function arrayFilter(arr, func) {
    let filteredArray = arr.filter(func);
    return filteredArray[0] ? filteredArray[0] : undefined;
  }
  
  // .FIND
  
  function arrayFilterFind(arr, func) {
    return arr.find(func);
  }
  

};
</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
Imperative Approach
arrayFilterImperative([1, 2, 3, 4], function(num){ return num % 2 === 0; })
pending…
.filter()
arrayFilter([1, 2, 3, 4], function(num){ return num % 2 === 0; })
pending…
.find()
arrayFilterFind([1, 2, 3, 4], function(num){ return num % 2 === 0; })
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