Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript for() vs for..in vs forEach() vs _.forEach() vs _.forEachRight()
(version: 3)
This benchmark tracks speed performance between two native iterator methods in JavaScript and Lodash forEach method.
Comparing performance of:
Native for() vs Native for..in vs Native forEach() vs Lodash _.forEach() vs Lodash _.forEachRight()
Created:
7 years ago
by:
Registered User
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>
Tests:
Native for()
let arr = [0,1,2,3,4,5,6,7,8,9]; let acum = 0; for (let i = 0; i < arr.length; i += 1) { acum += arr[i]; }
Native for..in
let arr = [0,1,2,3,4,5,6,7,8,9]; let acum = 0; for (let item in arr) { acum += item; }
Native forEach()
let arr = [0,1,2,3,4,5,6,7,8,9]; let acum = 0; arr.forEach(function (item) {acum += item});
Lodash _.forEach()
let arr = [0,1,2,3,4,5,6,7,8,9]; let acum = 0; _.forEach(arr, function (item) {acum += item});
Lodash _.forEachRight()
let arr = [0,1,2,3,4,5,6,7,8,9]; let acum = 0; _.forEachRight(arr, function (item) {acum += item});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Native for()
Native for..in
Native forEach()
Lodash _.forEach()
Lodash _.forEachRight()
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. **What is being tested?** The benchmark tests four different approaches to iterate over an array: 1. **Native `for` loop**: A traditional `for` loop using `i = 0; i < arr.length; i += 1`. 2. **Native `for...in` loop**: An iterator loop using `for (let item in arr) { ... }`. Note that this is not a standard JavaScript `for...in` loop, but rather an iterator that iterates over the array. 3. **Native `forEach()` method**: The built-in `Array.forEach()` method, which calls a callback function for each element in the array. 4. **Lodash `_forEach()` and `_forEachRight()` methods**: Two custom functions from Lodash, one iterating over the array from start to end (`_.forEach()`), and another iterating from end to start (`_.forEachRight()`). **Options compared** The benchmark compares the execution speed of these four approaches on a sample array `[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]`. **Pros and Cons** Here's a brief summary: * **Native `for` loop**: Simple and efficient, but may not be as readable or maintainable as other options. * **Native `for...in` loop**: Unconventional use of an iterator, which may lead to confusion. However, it can provide good performance since it doesn't require the V8 engine's iteration machinery. * **Native `forEach()` method**: A modern and expressive way to iterate over arrays, but may incur overhead due to its higher-level abstraction. * **Lodash `_forEach()` and `_forEachRight()` methods**: Convenient and readable functions from a popular utility library. However, they introduce additional dependencies and potential performance overhead. **Library usage** In this benchmark, Lodash is used as a utility library for two custom functions: `_forEach()` and `_forEachRight()`. These functions are likely used to simplify the iteration process over arrays. **Special JS features or syntax** None mentioned in this benchmark. However, it's worth noting that other benchmarks might use special JavaScript features like `let` or `const`, arrow functions, or modern iteration methods like `for...of` or `for...entries`. **Alternatives** Other alternatives for iterating over arrays include: * Using a library like `ramda` or `lodash.deepIterate` * Implementing your own custom iterator using JavaScript's built-in `Iterator` protocol * Utilizing third-party libraries like `js-array-utils` or `array-iter` These alternatives might offer different trade-offs in terms of performance, readability, and maintainability.
Related benchmarks:
Array.prototype.forEach vs Lodash forEach
lodash foreach vs for-of vs forEach
native for loop vs Array.prototype.forEach vs lodash forEach
lodash.foreach vs for-of vs array.forEach
Comments
Confirm delete:
Do you really want to delete benchmark?