<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);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Filter length | |
lodash sumBy | |
lodash filter chaining | |
manualMethod1 | |
manualMethod2 (with lambda function test) | |
manualMethod3 (for-each loop, with lambda function test) |
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 |
Let's break down the benchmark and explain what is being tested, compared, and their pros and cons.
Benchmark Overview
The benchmark tests the performance of three methods to count the number of elements in an array that meet a certain condition:
manualMethod1
)filter()
method with .length
property access (lodash filter().length
)sumBy()
method (lodash sumBy
)filter()
method with .size
property access (lodash filter().size
)manualMethod2
)manualMethod3
)What is being tested?
The benchmark tests the execution speed of these six methods on large arrays (500,000 elements). The results are reported in executions per second.
Comparison of options:
manualMethod1
) vs Manual loop-based method with lambda function test (manualMethod2
):manualMethod2
uses a lambda function as a test condition, which can be more readable and concise than a traditional if statement. Performance-wise, both methods should be similar, but manualMethod2
might have slight advantages due to the shorter code length.filter()
method with .length
property access (lodash filter().length
) vs Lodash sumBy()
method:sumBy()
is generally faster than filtering followed by length checking because it avoids an additional iteration and property lookup. On the other hand, filter()
can be more intuitive to use when dealing with large arrays.filter()
method with .size
property access (lodash filter().size
) vs Lodash sumBy()
method:Pros and cons of each approach:
filter()
method with .length
property access:sumBy()
method:sumBy
function and its assumptions.Lodash filter() method with .size property access
Pros:
Cons:
Overall, Lodash's optimized methods (filter()
and sumBy
) are generally faster than manual loop-based approaches. However, the choice of method depends on the specific use case, personal preference, and readability considerations.