Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
SUM with forEach vs lodash.map vs map fix
(version: 0)
Comparing performance of:
reduce vs Lodash map vs map vs forEach
Created:
7 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>
Tests:
reduce
let posts = [ {id: 1, upVotes: 2}, {id: 2, upVotes: 18}, {id: 3, upVotes: 1}, {id: 4, upVotes: 30}, {id: 5, upVotes: 50} ]; var sum = 0 sum = posts.reduce((s, p)=> s+=p.upVotes,0);
Lodash map
let posts = [ {id: 1, upVotes: 2}, {id: 2, upVotes: 18}, {id: 3, upVotes: 1}, {id: 4, upVotes: 30}, {id: 5, upVotes: 50} ]; var sum = 0 sum = _.map(posts, (element, i) => sum += element.upVotes)
map
let posts = [ {id: 1, upVotes: 2}, {id: 2, upVotes: 18}, {id: 3, upVotes: 1}, {id: 4, upVotes: 30}, {id: 5, upVotes: 50} ]; var sum = 0 sum = posts.map(element => { sum += element.upVotes; });
forEach
let posts = [ {id: 1, upVotes: 2}, {id: 2, upVotes: 18}, {id: 3, upVotes: 1}, {id: 4, upVotes: 30}, {id: 5, upVotes: 50} ]; var sum = 0 sum = posts.forEach(element => { sum += element.upVotes; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
reduce
Lodash map
map
forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (iPad; CPU OS 17_6_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/131.0.6778.73 Mobile/15E148 Safari/604.1
Browser/OS:
Chrome Mobile iOS 131 on iOS 17.6.1
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reduce
15209647.0 Ops/sec
Lodash map
4105505.0 Ops/sec
map
8542531.0 Ops/sec
forEach
11225934.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks, comparing the performance of different approaches to solve a specific problem. **Benchmark Definition JSON** The provided JSON represents a benchmark named "SUM with forEach vs lodash.map vs map fix". The benchmark consists of four test cases: 1. `reduce` 2. `Lodash map` 3. `map` (without Lodash) 4. `forEach` Each test case uses an array of objects (`posts`) to calculate the sum of the `upVotes` property. **Test Cases** Let's break down each test case: ### 1. `reduce` The `reduce` method applies a user-supplied function to each element in the array, accumulating a result. In this case, the accumulator (`sum`) is initialized to 0 and updated with the sum of the `upVotes` property for each object in the array. Pros: Efficient use of memory, no additional iterations required. Cons: Can be slower than other methods if the user-supplied function is complex or has many iterations. **Lodash `map`** The `Lodash map` method applies a callback function to each element in the array and returns an array of results. In this case, the callback function adds the sum of the `upVotes` property to the accumulator (`sum`) for each object in the array. Pros: Convenient and concise way to perform iterations, especially with Lodash's extensive library functions. Cons: Creates a new array with intermediate results, which can be slower than other methods. **Lodash `map` (with fix)** This test case uses a modified version of the `Lodash map` method that avoids creating an additional array. Instead, it directly updates the accumulator (`sum`) for each object in the array. Pros: Similar to Lodash's original implementation, but without creating an intermediate array. Cons: May be slower than the original Lodash `map` implementation due to the overhead of updating the accumulator manually. **`forEach`** The `forEach` method applies a callback function to each element in the array and returns no value. In this case, the callback function adds the sum of the `upVotes` property to the accumulator (`sum`) for each object in the array. Pros: Convenient way to perform iterations without creating an intermediate array. Cons: May be slower than other methods due to the overhead of calling a separate method for each iteration. **Libraries and Features** * Lodash is a popular JavaScript library that provides utility functions, including `map` and other iteration helpers. * No specific JavaScript features or syntax are used in these test cases.
Related benchmarks:
Array.prototype.map vs Lodash.map
Array.prototype.map vs Lodash.map 4.17.15
Map Comparison
Array.prototype.map vs Lodash.map on large data
array.map vs _.map
Comments
Confirm delete:
Do you really want to delete benchmark?