Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
sum array items
(version: 0)
Comparing performance of:
lodash sum vs native sum vs lodash for each
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var itemsArray = []; for (let i = 0; i < 100000; i++) { itemsArray.push({ used: i, free: i, total: i + i, }) }
Tests:
lodash sum
const data = { a: _.sumBy(itemsArray, item => item.used), b: _.sumBy(itemsArray, item => item.free), c: _.sumBy(itemsArray, item => item.total), } console.log(data)
native sum
let a = 0; let b = 0; let c = 0; itemsArray.forEach((item) => { a += item.used; b += item.free; c += item.total; }) console.log({a, b, c})
lodash for each
let a = 0; let b = 0; let c = 0; _.forEach(itemsArray, (item) => { a += item.used; b += item.free; c += item.total; }) console.log({a, b, c})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash sum
native sum
lodash for each
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 its various components. **Benchmark Definition JSON** The provided JSON represents a JavaScript microbenchmark that tests the performance of three different approaches to calculate the sum of items in an array: 1. **Lodash**: The `lodash` library is used for two functions: `sumBy` and `forEach`. `sumBy` calculates the sum of values in an array based on a provided function, while `forEach` iterates over an array and executes a callback function for each element. 2. **Native JavaScript**: The native approach uses basic JavaScript loops to iterate over the array and calculate the sums. **Options Compared** The benchmark compares the performance of Lodash (`lodash sum`, `lodash for each`) versus Native JavaScript (`native sum`). **Pros and Cons of Each Approach** 1. **Lodash** * Pros: + Provides a concise and readable way to perform calculations. + Reduces boilerplate code, making it more efficient. * Cons: + Requires an external library (lodash), which may introduce overhead. 2. **Native JavaScript** * Pros: + No external dependencies or overhead. + Can be optimized for specific use cases. * Cons: + Requires manual loop logic, making it more verbose and potentially less efficient. **Special JS Features or Syntax** The benchmark uses the following special features: 1. **Arrow functions**: Used in the `forEach` callback function to define a concise and readable way to iterate over the array. 2. **Template literals**: Used to create string values (e.g., `"a += item.used;"`) without concatenation. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a collection of functional programming helpers, including `sumBy` and `forEach`. Its primary purpose is to simplify common tasks and improve code readability. In this benchmark, Lodash is used for its `sumBy` function to calculate the sum of values in an array. **Other Alternatives** In addition to Native JavaScript and Lodash, there are other alternatives for calculating sums in arrays: 1. **Array.prototype.reduce()**: A native JavaScript method that can be used to reduce an array to a single value. 2. **Array.prototype.forEach()**: While not optimized for performance, `forEach` can still be used with a callback function to iterate over an array and calculate sums. These alternatives offer varying trade-offs between conciseness, readability, and performance compared to the Native JavaScript approach and Lodash implementation in this benchmark.
Related benchmarks:
Set iteration-fixed
Lodash reduce with native reduce
Lodash sumBy string vs array vs function
Compare Lodash and JS
Comments
Confirm delete:
Do you really want to delete benchmark?