Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4.1 Safari/605.1.15
Safari 17
Mac OS X 10.15.7
Desktop
11 months ago
Test name Executions per second
Filter length 210.1 Ops/sec
lodash sumBy 354.1 Ops/sec
lodash filter chaining 212.5 Ops/sec
manualMethod1 355.1 Ops/sec
manualMethod2 (with lambda function test) 358.8 Ops/sec
manualMethod3 (for-each loop, with lambda function test) 328.4 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 = 500000;
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);