Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs forEach vs for in vs for of vs lodash
(version: 0)
for vs forEach vs for in vs for of vs lodash
Comparing performance of:
forEach vs for in vs for vs for of vs lodash foreach
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>
Script Preparation code:
var arr = []; function doMath(a) { return Math.log(a); } for(i=0; i<10000; i++){ arr[i] = i + 1; }
Tests:
forEach
let sum = 0; arr.forEach((value) => { sum += doMath(value); });
for in
let sum = 0; for(const index in arr) { sum += doMath(arr[index]); }
for
let sum = 0; for(var i = 0; i < arr.length; i++) { const value = arr[i]; sum += doMath(value); }
for of
let sum = 0; for(const value of arr) { sum += doMath(value); }
lodash foreach
let sum = 0; _.forEach(arr, (value) => { sum += doMath(value); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
forEach
for in
for
for of
lodash foreach
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 dive into the world of JavaScript microbenchmarks. **Benchmark Overview** The provided benchmark tests four different methods for iterating over an array: `forEach`, `for in`, `for`, and `for of`. Additionally, it includes a fifth test case that uses Lodash's `_forEach` method. The goal is to compare the performance of each iteration method on a specific task: summing up the logarithm of each element in the array. **Iteration Methods** Here's a brief explanation of each iteration method: 1. **`forEach`**: This method calls a provided callback function once for each element in an array, with the current element as its argument. 2. **`for in`**: This method iterates over the properties of an object (in this case, the array's `length` property) using the prototype chain. It returns the value of each property as it is accessed. 3. **`for`**: Also known as a traditional loop, this method uses a manual index variable to access elements in an array. 4. **`for of`**: Introduced in ECMAScript 2015 (ES6), this method allows you to iterate over an array using a concise syntax that's similar to `forEach`. 5. **`lodash foreach`**: This is a custom implementation of the `forEach` method using Lodash. **Library: Lodash** Lodash is a popular JavaScript library that provides a lot of utility functions for tasks like array manipulation, string manipulation, and more. In this benchmark, Lodash's `_forEach` method is used to iterate over the array. Lodash provides a lot of convenience functions, but it also introduces additional overhead compared to native JavaScript methods. **Special JS Features** The test cases use some ES6 features: * `for of`: This syntax was introduced in ES6 and allows for concise iteration over arrays. * `_forEach` (Lodash): While not an officially recognized feature, this method is widely used and supported by many browsers. **Pros and Cons** Here's a brief summary of the pros and cons of each iteration method: 1. **`forEach`**: Pros: + Concise syntax + Wide browser support Cons: + May incur additional overhead due to function call 2. **`for in`**: Pros: + Fast iteration (since it doesn't require function calls) Cons: + Not designed for array iteration, may lead to unexpected behavior 3. **`for`**: Pros: + Fast iteration * Cons: + Manual index management can be error-prone 4. **`for of`**: Pros: + Concise syntax + Modern and widely supported (since ES6) Cons: + May incur additional overhead due to function call 5. **`lodash foreach`**: Pros: + Convenience function for common iteration use cases Cons: + Additional overhead due to Lodash library **Other Alternatives** For array iteration, some developers might consider using other libraries like: * `Array.prototype.map()` and `Array.prototype.forEach()`: These methods provide a more functional programming style of iteration. * `reduce()`: This method is useful for reducing an array to a single value. In general, the choice of iteration method depends on personal preference, performance considerations, and the specific requirements of your project.
Related benchmarks:
lodash.each vs Object.forEach test
for vs forEach vs for in vs for of vs lodash vs for reverse (2)
for vs forEach vs for in vs for of vs lodash vs for reverse (3)
Comparing sum of array elements between Native and lodash and some more for loops
Comments
Confirm delete:
Do you really want to delete benchmark?