<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var sum = 0;
function sumJS(parameters) {
parameters.forEach((n) => sum += n);
}
function sumLodash(parameters) {
_.forEach(parameters, (n) => sum += n);
}
sumJS(array)
sumLodash(array)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
JS sum | |
Lodash sum |
Test name | Executions per second |
---|---|
JS sum | 399549.3 Ops/sec |
Lodash sum | 344005.2 Ops/sec |
I'd be happy to help you understand what's being tested in this benchmark.
What is being tested?
MeasureThat.net is testing the performance of two different approaches for calculating the sum of an array: a custom implementation using JavaScript (JS) and an implementation using the Lodash library.
Options compared:
The benchmark is comparing the following options:
sumJS
that uses the forEach
method to iterate over the array elements and add them up.sumLodash
that uses the Lodash library's forEach
function to iterate over the array elements and add them up.Pros and cons of each approach:
forEach
.In general, the Lodash implementation is expected to perform better than the custom JavaScript implementation due to its optimized internal logic and caching mechanism.
Library used:
Lodash (version 4.17.5) is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, string manipulation, and more.
Special JS feature or syntax:
The benchmark uses the forEach
method, which is a built-in JavaScript method that allows iterating over an iterable object (such as an array). No special JavaScript features or syntax are required to run this benchmark.
Other alternatives:
If you're interested in exploring other alternatives for calculating the sum of an array, here are a few options:
map()
to create a new array with the squared values and then reducing it to calculate the sum.Keep in mind that these alternatives may have slightly different performance characteristics compared to the custom JavaScript implementation or the Lodash implementation.
I hope this explanation helps!