Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map reduce vs sumBy
(version: 0)
Comparing performance of:
Native vs Lodash
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
Script Preparation code:
var arr = [{ a: 1 }, { a: 5 }, { a: 11 }, { a: 55 }];
Tests:
Native
const total = arr.map(x => x.a).reduce((prev,curr) => prev + curr, 0);
Lodash
const total = _.sumBy(arr, x => x.a);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Native
Lodash
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. `Array.prototype.map()`: This function creates a new array with the results of applying a provided function to each element in the original array. 2. `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: ```javascript 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: 1. `_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: ```javascript 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.
Related benchmarks:
flatMap vs reduce
flatMap + reduce vs reduce + reduce
flatMap vs reduces
flatMap vs reduce small array
Comments
Confirm delete:
Do you really want to delete benchmark?