Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash vs for-of vs forEach3
(version: 0)
Comparing performance of:
lodash.forEach vs native for-of vs native for
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 = []; for (let i = 1; i<100000; i++) {arr.push(i)}
Tests:
lodash.forEach
_.forEach(arr, function(v) {v+v})
native for-of
for (const a of arr) {a+a}
native for
for (let i = 0; i < arr.length; i++) { arr[i] + arr[i] }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash.forEach
native for-of
native for
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash.forEach
1687.0 Ops/sec
native for-of
8141.1 Ops/sec
native for
2902.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON files and explain what's being tested. **Benchmark Definition** The `Benchmark Definition` represents a JavaScript function or block of code that is being tested for performance. In this case, there are three test cases: 1. `_.forEach(arr, function(v) {v+v})`: This is a benchmark definition using the Lodash library. It defines an array `arr` and uses the `forEach` method to iterate over it, performing a simple operation on each element (`v+v`). 2. `for (const a of arr) {a+a}`: This is a native JavaScript test case that uses the `for-of` loop syntax to iterate over the same array `arr`. The loop body performs the same operation as before (`a+a`). 3. `for (let i = 0; i < arr.length; i++) { arr[i] + arr[i] }`: This is another native JavaScript test case that uses a traditional `for` loop to iterate over the array `arr`. The loop body performs the same operation as before (`arr[i] + arr[i`). **Library: Lodash** The `_.forEach` method is part of the Lodash library, which provides a collection of high-order functions for functional programming. In this case, it's used to iterate over an array and perform a simple operation on each element. **Pros and Cons** * **Lodash `.forEach`:** This approach has some advantages: + It allows for more concise code and reduced boilerplate. + Lodash provides additional functionality and tools for working with arrays. * However, it also has some disadvantages: + It requires including an external library (in this case, Lodash). + The overhead of using a library might introduce additional latency. * **Native `for-of`:** This approach is simple, lightweight, and doesn't require any external libraries. However: + It's less concise than the Lodash version. + It requires more manual management of loop variables and iterations. * **Native `for` loop:** Similar to `for-of`, this approach is also lightweight but: + Even more verbose than `for-of`. + Requires more manual iteration management. **Considerations** When comparing these approaches, it's essential to consider the context in which they'll be used. For example: * If performance-critical code needs to iterate over large arrays, the native `for-of` or `for` loop might be a better choice. * If concise code is more important than performance, Lodash `.forEach` could be a better option. **Other Alternatives** If you're interested in exploring other alternatives, here are some options: * Other iterative methods: `forEach`, `map`, `reduce`, or even `filter`. * Different array data structures (e.g., `Set`, `Map`, `ArrayBuffer`) that might affect performance. * Optimizations for specific JavaScript engines or browsers. Overall, the choice of iteration method depends on the specific requirements and constraints of your project.
Related benchmarks:
lodash vs for-of vs forEach2
native for loop vs Array.prototype.forEach vs lodash forEach
lodash vs for-of vs forEach vs map v2
lodash.foreach vs for-of vs array.forEach
Comments
Confirm delete:
Do you really want to delete benchmark?