Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash each vs for of loop
(version: 0)
Comparing performance of:
lodash.forEach vs native
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/lodash/4.16.0/lodash.min.js"></script>
Script Preparation code:
var values = []; for (let i = 0; i < 100_000; i++) { values.push({ a: i }); }
Tests:
lodash.forEach
var count = 0; _.each(values, function(v,i) { count++; })
native
var count = 0; for (const v of values) { count++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash.forEach
native
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/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash.forEach
2080.1 Ops/sec
native
15160.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark definition and test cases to understand what's being tested. **Benchmark Definition** The benchmark is testing two approaches: 1. **_.each(values, function(v,i) { count++; })**: This uses the Lodash library, specifically its `forEach` method. The `_.each` method iterates over an array using a callback function. 2. **for (const v of values) { count++; }**: This is a native JavaScript approach using a for-of loop to iterate over the array. **Lodash Library** The Lodash library is a popular utility library for JavaScript that provides a lot of helper functions. In this case, `_` is an alias for the Lodash root object, and `lodash.forEach` is a function that iterates over an array using a callback function. **Native JavaScript Approach** The native approach uses a for-of loop to iterate over the array. A for-of loop allows you to iterate over arrays or other iterable objects without having to specify the length of the array beforehand. **Pros and Cons** Here are some pros and cons of each approach: * **_.each(values, function(v,i) { count++; })**: Pros: + Concise and easy to read + Lodash provides a familiar interface for iterating over arrays Cons: + Adds an extra dependency (the Lodash library) + May incur overhead due to the additional function call * **for (const v of values) { count++; }**: Pros: + No dependencies or overhead + Can be faster since it doesn't involve a function call Cons: + Requires understanding of for-of loops and array destructuring + Can be less readable if not familiar with the syntax **Other Considerations** * The benchmark only tests these two approaches, so we can't compare performance to other methods like `forEach`, `map`, or `reduce`. * The test uses a small array size (100,000 elements), which might not accurately represent larger datasets. * The benchmark doesn't account for potential side effects of the iterations (e.g., modifying the original array). **Latest Benchmark Results** The latest results show that the native approach (`for (const v of values) { count++; }`) outperforms the Lodash approach (`_.each(values, function(v,i) { count++; })`). This suggests that, in this specific scenario, using a native for-of loop might be faster. As for special JavaScript features or syntax, there's none mentioned in this benchmark.
Related benchmarks:
lodash.each vs Object.forEach vs Native for
native for loop vs Array.prototype.forEach vs lodash forEach
lodash vs for-of vs forEach vs map
lodash vs for-of vs forEach vs map v2
Comments
Confirm delete:
Do you really want to delete benchmark?