Filter

Benchmark created by Eric Elliott on


Setup

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;
  }, []);

Teardown



            [w,x,y,z] = [[],[],[],[]];
        
  

Test runner

Ready to run.

Testing in
TestOps/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, []);
ready
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, []);
ready
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, []);
ready
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, []);
ready

Revisions

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

  • Revision 1: published by Eric Elliott on