Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-vs-foreach-vs-forof
(version: 0)
Comparing performance of:
forEach vs for vs for of vs arrowForEach vs forBackward vs reverse while
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for (var i = 0; i < 1000; i++) { arr[i] = i; } function someFn(i) { return i * 3 * 8; }
Tests:
forEach
arr.forEach(function (item){ someFn(item); });
for
for (var i = 0, len = arr.length; i < len; i++) { someFn(arr[i]); }
for of
for(xxx of arr){ someFn(xxx); }
arrowForEach
arr.forEach((item)=>{ someFn(item); });
forBackward
for (var i=arr.length-1; i>=0; i--) { someFn(arr[i]); }
reverse while
var i = arr.length; while (i--) { someFn(arr[i]); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
forEach
for
for of
arrowForEach
forBackward
reverse while
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 benchmark and its results. **Benchmark Overview** The `for-vs-foreach-vs-forof` benchmark compares the performance of three different loops: 1. `forEach`: Iterates over an array using the `Array.prototype.forEach()` method, which calls a callback function for each element in the array. 2. `for`: A traditional `for` loop that iterates over an array using its length property and index variable. 3. `for-of`: The newer `for...of` loop syntax, introduced in ECMAScript 2015 (ES6), which is used to iterate over iterable objects like arrays. **Library Used** None, this benchmark does not use any external libraries. **Special JS Features/Syntax** The `for-of` loop uses the new `for...of` syntax, which is a modern JavaScript feature introduced in ES6. This loop iterates over an array by simply using its elements as values in the iteration. **Pros and Cons of Different Approaches** 1. **forEach**: This approach can be less efficient than traditional loops because it involves more overhead due to function calls. * Pros: Cleaner and more concise code, easier to write and read. * Cons: May incur additional function call overhead, potentially slower performance. 2. **for**: Traditional `for` loops are generally faster and more efficient because they don't involve the overhead of function calls. * Pros: Faster performance, lower memory usage. * Cons: More verbose code, harder to read and maintain. 3. **for-of**: The new `for...of` loop syntax is a compromise between `forEach` and traditional `for` loops. * Pros: Cleaner and more concise code than traditional `for` loops, similar performance to traditional loops. * Cons: Requires support for modern JavaScript versions (ES6+), may not be as well-known or widely supported. **Other Considerations** * The results of this benchmark are specific to the given JavaScript engine (V8) version and platform (Mac OS X 10.13.2). * Other JavaScript engines, browsers, or platforms may exhibit different performance characteristics for these loops. * This benchmark only compares the performance of `forEach`, traditional `for` loops, and `for...of` loops; other loop constructs, like `while` loops or `map()`/`filter()` methods, are not included. **Alternatives** If you need to compare the performance of different loop constructs, you can modify this benchmark by adding additional test cases for other loop types, such as: * `while` loops * `map()`/`filter()` methods (which don't involve traditional looping) * Other modern JavaScript features like ` reduce()`, `find()`, or `every()` Keep in mind that each of these alternatives will require its own benchmarking code and results.
Related benchmarks:
index loop vs for-of loop vs foreach vs map
Array loop: forEach vs for vs map vs for of entries
Array loop vs foreach vs map e
Array loop vs foreach vs map with large array
Array loop vs foreach vs for_of
Comments
Confirm delete:
Do you really want to delete benchmark?