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
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]); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
forEach
for
for of
arrowForEach
forBackward
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):
Measuring the performance of different JavaScript iteration constructs is crucial for optimizing code and improving application performance. **Benchmark Overview** The provided benchmark measures the execution time of four different iteration approaches in JavaScript: 1. `forEach` 2. Traditional `for` loop 3. `for...of` loop (introduced in ECMAScript 2015) 4. Arrow function-based `forEach` These iterations are executed on an array with 1000 elements, and the benchmarked code is defined in the script preparation code. **Iteration Approaches** Here's a brief description of each approach: 1. **`forEach`**: This method iterates over an array using a callback function. The callback function receives each element as an argument. 2. **Traditional `for` loop**: This classic loop construct uses an index variable to iterate over the array elements. 3. **`for...of` loop**: Introduced in ECMAScript 2015, this loop type iterates over iterable objects using a declarative syntax. The loop variable is automatically bound to each element of the array. 4. **Arrow function-based `forEach`**: This approach uses an arrow function as the callback function for the `forEach` method. **Comparison of Iteration Approaches** Here are some pros and cons of each approach: * **`forEach`**: + Pros: concise, easy to read, and widely supported. + Cons: can be slower due to callback function overhead. * **Traditional `for` loop**: + Pros: fine-grained control over iteration, no callback function overhead. + Cons: verbose, less readable than other approaches. * **`for...of` loop**: + Pros: concise, easy to read, and modern syntax. + Cons: requires ECMAScript 2015 support, not as widely supported as `forEach`. * **Arrow function-based `forEach`**: + Pros: concise, easy to read, and can be faster due to lambda function optimization. + Cons: may not work in older browsers or environments. **Library and Special JS Feature** In this benchmark, no libraries are used. However, the `for...of` loop relies on ECMAScript 2015 support, which might not be available in all browsers or environments. **DevicePlatform and OperatingSystem** The provided benchmark result shows data from a Chrome 62 browser running on Mac OS X 10.12.6. The results may vary depending on the platform and browser used. **Other Alternatives** If you're interested in exploring alternative iteration approaches, here are some options: 1. **`while` loop**: Another traditional loop construct that iterates over an array using a condition variable. 2. **Generator functions**: A newer way to iterate over arrays, providing more control over iteration and yielding values. 3. **Set iterators**: A modern way to iterate over sets, providing fast lookup and iteration capabilities. When choosing an iteration approach, consider the trade-offs between readability, performance, and compatibility with different browsers and environments.
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?