Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash forEach vs for in loop
(version: 0)
Comparing performance of:
foreach vs for in
Created:
4 years ago
by:
Registered User
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 = [{a: 30310}, {b: 100303}, {c: 3040494}]
Tests:
foreach
var count = 0; _.forEach(values, function(v,i) { if (v.a != null) { count++; } })
for in
var count = 0; for(var value in values) { if (value.a != null) { count++; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
foreach
for in
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
foreach
55161120.0 Ops/sec
for in
20041340.0 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, the options compared, their pros and cons, and other considerations. **What is being tested?** The benchmark compares two approaches for iterating over an array: `lodash.forEach()` and a traditional `for` loop with `in`. The test case uses a small dataset of three objects with properties `a`, `b`, and `c`. **Options compared:** 1. **Lodash `forEach()`**: A function that iterates over an array, executing the provided callback for each element. 2. **Traditional `for` loop with `in`**: A classic loop structure that iterates over an object's properties using the `in` operator. **Pros and cons of each approach:** 1. **Lodash `forEach()`** * Pros: + Concise code + Easy to read and maintain + Handles array iteration out-of-the-box * Cons: + Requires an external library (lodash) + May have a performance overhead due to the callback function 2. **Traditional `for` loop with `in`** * Pros: + No external dependencies or overhead + Can be faster for small datasets * Cons: + More verbose code + Requires manual management of iteration (e.g., checking for existence) **Library: Lodash** Lodash is a popular JavaScript library that provides a collection of functional programming helpers. In this case, the `forEach()` function is used to iterate over the `values` array. **Special JS feature or syntax** There are no specific features or syntax mentioned in the benchmark definition or test cases. **Other alternatives** For iterating over arrays, other approaches can be considered: 1. **Array.prototype.forEach()**: A built-in method that iterates over an array. 2. **Map and callback functions**: Using a `Map` object to store iteration results and applying a callback function for each value. 3. **While loops with index management**: Using a traditional `while` loop to iterate over the array, managing indices manually. These alternatives may have different trade-offs in terms of performance, conciseness, and maintainability, but can be considered depending on the specific use case and requirements. The benchmark result shows that Chrome 92 executes the `for in` loop faster than the `forEach()` function, but with a larger number of executions per second. This may indicate that the traditional `for` loop approach has an advantage in this particular scenario, despite being more verbose.
Related benchmarks:
lodash.each vs Object.forEach vs Native for
Lodash foreach vs native foreach
lodash forEach vs for i loop modified
lodash .foreach vs native foreach vs native forof
lodash .foreach vs native foreach vs native for loop
Comments
Confirm delete:
Do you really want to delete benchmark?