<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
var arr = [{ a: 1 }, { a: 5 }, { a: 11 }, { a: 55 }];
const total = arr.map(x => x.a).reduce((prev,curr) => prev + curr, 0);
const total = _.sumBy(arr, x => x.a);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Native | |
Lodash |
Test name | Executions per second |
---|---|
Native | 10486937.0 Ops/sec |
Lodash | 3640916.8 Ops/sec |
Let's break down the provided benchmark and explain what is being tested.
Benchmark Overview
The benchmark compares the performance of two approaches to calculate the sum of all "a" values in an array: the native JavaScript method using map()
and reduce()
, and the Lodash library method using _sumBy()
.
Native Method
The native method uses two built-in functions:
Array.prototype.map()
: This function creates a new array with the results of applying a provided function to each element in the original array.Array.prototype.reduce()
: This function applies a provided function to an accumulator and each element in the array (from left to right) to reduce it to a single value.The benchmark measures how long it takes for this combination of functions to execute the following code:
const total = arr.map(x => x.a).reduce((prev, curr) => prev + curr, 0);
This is the native JavaScript way of performing the calculation without relying on any external library.
Lodash Method
The Lodash method uses a single function from the Lodash library:
_sumBy()
: This function takes an array and a function as arguments and returns the sum of all elements that pass the test implemented by the provided function.In this case, the Lodash method is used to calculate the sum of all "a" values in the array with the following code:
const total = _.sumBy(arr, x => x.a);
This approach relies on an external library (Lodash) to perform the calculation.
Pros and Cons
Native Method: Pros:
No reliance on external libraries, which can reduce overhead.
Can be optimized by the JavaScript engine for specific use cases. Cons:
May require more code and explicit function chaining.
Performance can vary depending on the specific browser or environment.
Lodash Method: Pros:
Simplifies the calculation by reducing the need for explicit function chaining.
Provides a pre-built function that is likely to be optimized for performance. Cons:
Requires an external library, which introduces overhead.
May not be as performant as the native method due to the added layer of indirection.
Other Considerations
Both approaches assume that the input array arr
contains objects with an "a" property. The benchmark does not check for this assumption or provide any error handling.
If the input data is sparse (i.e., some elements do not have the "a" property), either method may produce incorrect results or throw errors.
Library Explanation
The Lodash library provides a wide range of useful functions for common tasks, such as array manipulation, object manipulation, and functional programming utilities. In this case, _sumBy()
is a simple but convenient way to calculate the sum of all elements that pass a test implemented by a provided function.
Special JS Feature or Syntax
This benchmark does not use any special JavaScript features or syntax beyond the standard map()
, reduce()
, and arrow functions.