Filter

JavaScript performance comparison

Test case created by Eric Elliott

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
  var [w,x,y,z] = [[],[],[],[]];
  
  var cFilter = (fn, arr) => arr.reduce((newArr, item) => {
    return fn(item) ? newArr.concat([item]) : newArr;
  }, []);
  
  var sFilter = (fn, arr) => arr.reduce((newArr, item) => {
    return fn(item) ? [...newArr, item] : newArr;
  }, []);
  
  var pFilter = (fn, arr) => arr.reduce((newArr, item) => {
    if (fn(item)) {
      newArr.push(item);
      return newArr;
    }
    return newArr;
  }, []);
  
  var aFilter = (fn, arr) => arr.reduce((newArr, item) => {
    if (fn(item)) {
      newArr[newArr.length] = item;
      return newArr;
    }
    return newArr;
  }, []);

};

Benchmark.prototype.teardown = function() {
  [w,x,y,z] = [[],[],[],[]];

};
</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
concat
w = cFilter(n => n >= 2, a);
x = cFilter(n => n % 2 === 0, a);
y = cFilter(n => n === n, a);
z = cFilter(n => n === n, []);
pending…
spread
w = sFilter(n => n >= 2, a);
x = sFilter(n => n % 2 === 0, a);
y = sFilter(n => n === n, a);
z = sFilter(n => n === n, []);
pending…
push
w = pFilter(n => n >= 2, a);
x = pFilter(n => n % 2 === 0, a);
y = pFilter(n => n === n, a);
z = pFilter(n => n === n, []);
pending…
assign
w = aFilter(n => n >= 2, a);
x = aFilter(n => n % 2 === 0, a);
y = aFilter(n => n === n, a);
z = aFilter(n => n === n, []);
pending…

Revisions

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

0 Comments