Test name | Executions per second |
---|---|
Filter length | 150.7 Ops/sec |
lodash sumBy | 171.4 Ops/sec |
lodash filter chaining | 151.3 Ops/sec |
manualMethod1 | 341.0 Ops/sec |
manualMethod2 (with lambda function test) | 340.8 Ops/sec |
manualMethod3 (for-each loop, with lambda function test) | 329.7 Ops/sec |
<script src="https://cdn.jsdelivr.net/lodash/4.17.21/lodash.min.js"></script>
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;
}
const x = _.filter(window.array, i => i > .5).length;
return x;
const x = _.sumBy(window.array, i => i > .5 ? 1 : 0);
return x;
const x = _(window.array).filter(i => i > .5).size();
return x;
return manualMethod1(window.array);
return manualMethod2(window.array, i => i > .5);
return manualMethod3(window.array, i => i > .5);