Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:98.0) Gecko/20100101 Firefox/98.0
Firefox 98
Mac OS X 10.15
Desktop
2 years ago
Test name Executions per second
Filter length 3658686.2 Ops/sec
lodash sumBy 4611254.0 Ops/sec
lodash filter chaining 1018792.1 Ops/sec
manualMethod1 6866455.5 Ops/sec
manualMethod2 (with lambda function test) 6364288.0 Ops/sec
manualMethod3 (for-each loop, with lambda function test) 4468476.5 Ops/sec
HTML Preparation code:
AخA
 
1
<script src="https://cdn.jsdelivr.net/lodash/4.17.21/lodash.min.js"></script>
Script Preparation code:
x
 
const arraySize = 10;
window.array = _.range(arraySize).map(() => Math.random());
function manualMethod1(array) {
  let x = 0;
  let len = array.length;
  for (let i = 0; i < len; i++) {
    if (array[i] > .5) {
      x++;
    }
  }
  return x;
}
function manualMethod2(array, test) {
  let x = 0;
  let len = array.length;
  for (let i = 0; i < len; i++) {
    if (test(array[i])) {
      x++;
    }
  }
  return x;
}
function manualMethod3(array, test) {
  let x = 0;
  for (let i of array) {
    if (test(i)) {
      x++;
    }
  }
  return x;
}
Tests:
  • Filter length

     
    const x = _.filter(window.array, i => i > .5).length;
    return x;
  • lodash sumBy

     
    const x = _.sumBy(window.array, i => i > .5 ? 1 : 0);
    return x;
  • lodash filter chaining

     
    const x = _(window.array).filter(i => i > .5).size();
    return x;
  • manualMethod1

     
    return manualMethod1(window.array);
  • manualMethod2 (with lambda function test)

     
    return manualMethod2(window.array, i => i > .5);
  • manualMethod3 (for-each loop, with lambda function test)

     
    return manualMethod3(window.array, i => i > .5);