Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_.forEach vs for of
(version: 1)
Comparing performance of:
_.foreach vs for of
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
Script Preparation code:
var data = []; data.length = 50000; for (var i = 0; i < data.length; ++i) { data[i] = { key: 1000 + i * 10, something: "abc_" + i }; }
Tests:
_.foreach
_.forEach(data, d => { });
for of
for (var d of data) { }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.foreach
for of
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
_.foreach
6348.8 Ops/sec
for of
83174.2 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON compares two different approaches for iterating over an array of objects in JavaScript: using Lodash's `_.forEach` method and the ES6 `for...of` loop. ### Options Compared 1. **Lodash `_.forEach` Method**: - **Test Case**: `_.forEach(data, d => { });` - **Description**: This method is part of Lodash, a popular utility library that provides various functions for common programming tasks. Here, `_.forEach` is used to iterate over each element in the `data` array, executing a provided function for each element. 2. **ES6 `for...of` Loop**: - **Test Case**: `for (var d of data) { }` - **Description**: This is a built-in JavaScript syntax introduced in ECMAScript 2015 (ES6) for iterating over iterable objects (like arrays). The `for...of` loop allows developers to iterate through the values of the iterable without needing to manage an index explicitly. ### Performance Results From the benchmark results: - The `for...of` loop achieved **83,174** executions per second. - The Lodash `_.forEach` method achieved **6,349** executions per second. ### Pros and Cons #### Lodash `_.forEach` - **Pros**: - Consistency: Lodash provides a consistent API that can be more readable to some developers, especially those familiar with functional programming paradigms. - Chainable: Lodash functions can be easily chained with other Lodash operations, enabling more expressive and concise code. - **Cons**: - Overhead: Using Lodash introduces additional overhead since it involves function calls and relies on an external library, which can slow down execution compared to native solutions. - Bundle Size: Including Lodash can increase the bundle size of your application, especially if only a few functions are used. #### ES6 `for...of` - **Pros**: - Performance: The native `for...of` loop is generally more performant, as shown by the benchmark results. It has lower overhead and is optimized for iterations in modern JavaScript engines. - Simplicity: Being built into the language, it requires no additional libraries and has a straightforward syntax, making it accessible for all developers. - **Cons**: - Limited Functionality: Unlike Lodash, the `for...of` loop lacks some of the functional programming utilities that might simplify certain tasks when dealing with arrays and objects. - Compatibility: Older browsers that do not support ES6 will not recognize `for...of`, although this is less of an issue with modern JavaScript development practices and tooling. ### Other Considerations - **Alternatives**: - **`forEach` method**: Built into arrays, `data.forEach(d => { })` is another alternative, similar to Lodash's function. - **`map`, `filter`, etc.**: These higher-order functions can also be used for iterations depending on the desired outcome (transforming or filtering data rather than merely processing each item). - **Regular `for` loop**: Traditional `for` loops provide a way to iterate while maintaining more control over the iteration index, which can sometimes be advantageous. Ultimately, the choice between `_.forEach`, `for...of`, and other alternatives depends on the specific requirements of the task, performance considerations, and the constraints of the project including library usage, browser compatibility, and developer familiarity.
Related benchmarks:
for loop vs. lodash range foreach
for loop vs. lodash range foreach 2
for loop vs. lodash range foreach latest
for vs lodash foreach
for loop vs. lodash range foreach - ks
Lodash forOwn vs Native keys and forEach
Lodash forOwn vs native Object.values and forEach
_.forIn vs for in
for loop vs. lodash range foreach [2022-11-17]
Comments
Confirm delete:
Do you really want to delete benchmark?