Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash forEach vs for i loop forof1
(version: 0)
Comparing performance of:
lodash.forEach vs native
Created:
5 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 = [{ a: 30310 }, { b: 100303 }, { c: 3040494 }] var valArr = []; for (let i = 0; i < 20000; i++) { valArr.push({ a: 1, b: 2, c: 3 }); }
Tests:
lodash.forEach
var count = 0; _.forEach(valArr, function(v) { if (v.a != null) { count++; } })
native
var count = 0; for (let v of valArr) { if (v.a != null) { 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:
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 JSON and explain what is tested, compared options, pros and cons of each approach, and other considerations. **Benchmark Definition** The benchmark definition consists of two parts: `Script Preparation Code` and `Html Preparation Code`. The `Script Preparation Code` defines a JavaScript array `valArr` with 20,000 elements and an empty array `valArr` to be iterated over. It also initializes a counter variable `count`. The `Html Preparation Code` includes the URL of the Lodash library, which is used in one of the benchmark test cases. **Individual Test Cases** There are two individual test cases: 1. **Lodash forEach**: This test case uses the `_` (underscore) object from the Lodash library to iterate over the `valArr` array using the `forEach` method. The code increments a counter variable `count` whenever an element's property `a` is not null. 2. **Native**: This test case uses a traditional `for...of` loop to iterate over the `valArr` array, incrementing the same counter variable `count` as in the Lodash `forEach` test case. **Comparison** The two test cases compare the performance of using a library (`Lodash`) versus a native JavaScript implementation (`Native`). The Lodash `forEach` method is expected to be slower than the native `for...of` loop due to the overhead of calling a function for each iteration, whereas the native approach avoids this overhead. **Pros and Cons** * **Lodash forEach**: Pros: + Simplifies code by providing a concise way to iterate over arrays. + Reduces boilerplate code. Cons: + Adds an external library dependency, which may introduce additional overhead or security risks. + May be slower due to the function call overhead. * **Native**: Pros: + Faster execution, as it avoids the function call overhead. + No external library dependency. Cons: + Requires manual loop logic, which can be error-prone and less readable. **Other Considerations** * The benchmark assumes that the Lodash library is already included in the browser or context, which may not always be the case. In a real-world scenario, you might need to include the library explicitly. * The test cases do not account for other factors that can affect performance, such as: + Array length and type + Data distribution (e.g., random vs. sequential) + Browser version and platform-specific optimizations **Alternatives** Other approaches to iterating over arrays in JavaScript include: 1. **Array.prototype.forEach**: A built-in method that is similar to Lodash's `forEach`, but without the need for an external library. 2. **Array.prototype.map() + Array.prototype.every()**: Combining `map()` and `every()` can provide a more efficient way to iterate over arrays, especially when filtering or transforming data. 3. **Custom loop implementations**: Implementing custom loops using `while` or `do-while` statements can be an alternative to the native `for...of` approach. In summary, the benchmark highlights the trade-offs between using external libraries and native JavaScript implementation for array iteration. While Lodash's `forEach` provides a concise way to iterate over arrays, it may come at the cost of performance due to function call overhead. The native `for...of` loop, on the other hand, offers faster execution but requires manual loop logic and no external library dependency.
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 v2
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?