Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
native for loop vs Array.prototype.forEach vs lodash forEach
(version: 0)
Comparing performance of:
for loop vs Array.prototype.forEach vs lodash each
Created:
6 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 arr = _.range(10000)
Tests:
for loop
for(let i = 0; i < arr.length; i++) {}
Array.prototype.forEach
arr.forEach(ele => {})
lodash each
_.forEach(arr, ele => {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for loop
Array.prototype.forEach
lodash each
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for loop
55300.3 Ops/sec
Array.prototype.forEach
256202.3 Ops/sec
lodash each
20426.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark is comparing three approaches to iterate over an array: 1. **Native For Loop**: A traditional for loop that uses a counter variable (`i`) to access each element in the array. 2. **Array.prototype.forEach**: A method on the Array prototype that iterates over the elements of an array, executing a callback function for each element. 3. **Lodash `forEach`**: A utility function from the Lodash library that applies a given function to every element in an array. **Options Compared** The benchmark is comparing these three options based on their performance: * Native For Loop: Uses a traditional loop with a counter variable, which can be slower due to the overhead of incrementing the counter and checking if it's within bounds. * Array.prototype.forEach: Uses the built-in method for iterating over arrays, which is optimized for performance. However, it may have additional overhead due to the function call and argument passing. * Lodash `forEach`: Uses a third-party library that provides an optimized implementation of the for-each loop. **Pros and Cons** Here are some pros and cons of each approach: 1. **Native For Loop**: * Pros: Easy to implement, lightweight, and doesn't require additional libraries. * Cons: Can be slower due to counter incrementation and bounds checking. 2. **Array.prototype.forEach**: * Pros: Optimized for performance, built-in method, and concise syntax. * Cons: May have additional overhead due to function call and argument passing. 3. **Lodash `forEach`**: * Pros: Optimized implementation, fast execution time, and convenient syntax. * Cons: Requires an additional library dependency. **Library Used** The benchmark uses the Lodash library, which is a popular utility library for JavaScript. In this case, it's used to provide a consistent implementation of the `forEach` method across different environments. **Special JS Feature or Syntax** There are no special JavaScript features or syntaxes being tested in this benchmark. The focus is on comparing the performance of three basic iteration approaches. **Other Alternatives** If you're looking for alternative libraries or implementations, here are a few options: * **ES6 `for...of` loop**: A newer way to iterate over arrays using a for-of loop. * **Native `Array.prototype.map()` and `reduce()` methods**: Alternative methods for transforming and reducing arrays, which may be faster than the for-each loop in some cases. Keep in mind that the choice of iteration approach depends on your specific use case and performance requirements. This benchmark provides a useful comparison of three common approaches, but you should consider other factors when deciding on an implementation.
Related benchmarks:
Array.prototype.forEach vs Lodash forEach
lodash.each vs Object.forEach vs Native for
lodash _.forEach vs native Array.prototype.forEach
lodash.foreach vs for-of vs array.forEach
Comments
Confirm delete:
Do you really want to delete benchmark?