Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Benchmark For
(version: 0)
Foreach(lodash) vs ForOf vs ForIn vs For(native)
Comparing performance of:
Foreach(lodash) vs ForOf(native) vs ForIn(native) vs For(native)
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var list = Array.from({ length: 10000 }).map((value, i) => i);
Tests:
Foreach(lodash)
_.each(list, function(value,key) {})
ForOf(native)
for (const element of list) { }
ForIn(native)
for (const key in list) { const element = list[key]; }
For(native)
for (let index = 0; index < list.length; index++) { const element = list[index]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Foreach(lodash)
ForOf(native)
ForIn(native)
For(native)
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 explain what's being tested. **Benchmark Overview** The benchmark compares the performance of four different loop constructs in JavaScript: 1. `foreach` with Lodash (`.each`) 2. `for...of` native syntax (`for (const element of list) { ... }`) 3. `for...in` native syntax (`for (const key in list) { const element = list[key]; }`) 4. Traditional `for` loop with indexing (`for (let index = 0; index < list.length; index++) { const element = list[index]; }`) **Options Compared** The benchmark is comparing the execution speed of each loop construct on a large array of 10,000 elements. **Pros and Cons of Each Approach** 1. **Foreach with Lodash (.each)**: * Pros: Easy to read and write, especially for developers familiar with functional programming. * Cons: May incur overhead due to the use of a library, which can introduce additional dependencies and potential performance issues. 2. **For...of native syntax**: * Pros: Native implementation, minimal overhead, easy to read and write. * Cons: Only supported in modern browsers (ECMAScript 2015+). 3. **For...in native syntax**: * Pros: Also native implementation, similar performance characteristics as `for...of`. * Cons: May iterate over the object's prototype chain, which can lead to unexpected behavior if not used carefully. 4. **Traditional For loop with indexing**: * Pros: Wide support across older browsers and environments, easy to understand. * Cons: More verbose and less readable than other options. **Library Used** The benchmark uses Lodash's `.each` function, which is a utility function for iterating over arrays. **Special JS Feature or Syntax** None of the test cases use special JavaScript features or syntax that would impact their execution speed. **Other Alternatives** If `foreach` with Lodash was not used, other alternatives could include: * Using `Array.prototype.forEach()` directly (not shown in this benchmark) * Implementing a custom loop using a callback function * Using a library like Lo-Dash's `forEachRight` or `eachRight` Keep in mind that the performance differences between these options may vary depending on the specific use case and environment. The benchmark provides valuable insights into the performance characteristics of each loop construct, allowing developers to make informed decisions about which approach to use in their code.
Related benchmarks:
Array.prototype.forEach vs Lodash forEach
lodash foreach vs for-of vs forEach
native for loop vs Array.prototype.forEach vs lodash forEach
map vs forEach Chris
lodash.foreach vs for-of vs array.forEach
Comments
Confirm delete:
Do you really want to delete benchmark?