Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Underscore Each vs Lodash Each vs Native forEach
(version: 0)
Comparing performance of:
Underscore each vs Lodash each vs Native forEach
Created:
3 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 src='https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js'></script>
Script Preparation code:
var data = Array(10000).fill();
Tests:
Underscore each
_.each(data, ele => { console.log(ele) })
Lodash each
_.each(data, ele => { console.log(ele) })
Native forEach
data.forEach(ele => { console.log(ele) })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Underscore each
Lodash each
Native forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Underscore each
22.9 Ops/sec
Lodash each
22.6 Ops/sec
Native forEach
25.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and analyzed. **Benchmark Overview** The benchmark measures the performance of three different approaches to iterate over an array: 1. **Underscore each**: Uses the underscore library's `each` function to iterate over the array. 2. **Lodash each**: Uses the lodash library's `each` function to iterate over the array. 3. **Native forEach**: Uses the native JavaScript `forEach` method to iterate over the array. **Comparison of Approaches** Here's a brief overview of each approach and their pros and cons: * **Underscore each**: This approach uses the underscore library, which provides a more functional programming style of iteration. The `each` function takes two arguments: the array and a callback function that will be executed for each element. This approach is more concise but may incur overhead due to the additional library. + Pros: concise, easy to read + Cons: adds dependency on underscore library, potential performance overhead * **Lodash each**: Similar to underscore each, this approach uses the lodash library's `each` function. Lodash provides a more extensive set of utility functions for functional programming, which can be beneficial in certain scenarios. + Pros: provides additional utility functions, concise + Cons: adds dependency on lodash library, potential performance overhead * **Native forEach**: This approach uses the native JavaScript `forEach` method, which is the most lightweight and efficient way to iterate over an array. However, it may not be as concise or readable as the other two approaches. + Pros: lightweight, efficient + Cons: less concise, potentially harder to read **Library Considerations** In both "Underscore each" and "Lodash each" test cases, we're using external libraries to provide a functional programming style of iteration. These libraries add dependencies and potential overhead, but also offer additional utility functions that can simplify code. **Native JavaScript Syntax** The `forEach` method in the native JavaScript implementation is not explicitly tested as part of this benchmark, but it's worth noting that `forEach` is a built-in method that's designed for performance-critical code. **Alternative Approaches** Other approaches to iterate over an array might include: * Using a traditional loop with an index variable (`for (var i = 0; i < array.length; i++) { ... }`) * Using the Array.prototype.reduce() method * Using the Array.prototype.map() method However, these alternatives are likely to be less efficient than using `forEach` or similar methods. **Benchmark Preparation Code** The script preparation code creates an array of 10,000 elements and assigns it to the `data` variable. The HTML preparation code includes links to the underscore and lodash libraries, which are loaded before running the benchmark. Overall, this benchmark provides a useful comparison of different approaches to iterating over an array in JavaScript, highlighting the trade-offs between conciseness, readability, and performance.
Related benchmarks:
lodash.each vs Object.forEach
Lodash _.forEach vs Object forEach
benchmark--------7
lodash vs for-of vs forEach5453
Comments
Confirm delete:
Do you really want to delete benchmark?