Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For loop differences
(version: 0)
Comparing performance of:
for vs for..of vs Array#forEach
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var noop = (...args) => void args; var array = Array.from({ length: 10_000 }, (_, idx) => idx);
Tests:
for
for (let idx = 0; idx < array.length; idx++) { noop(array[idx]); }
for..of
for (const item of array) { noop(item); }
Array#forEach
array.forEach((item) => noop(item));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for
for..of
Array#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 on MeasureThat.net. **Benchmark Definition** The benchmark measures the performance difference between three different approaches to iterate over an array: 1. **For loop**: A traditional, explicit loop using a `for` statement. 2. **For...of loop**: A newer, more concise way to iterate over arrays and other iterables using a `for...of` statement. 3. **Array#forEach**: A built-in method for iterating over an array. **Options Compared** The benchmark compares the performance of these three approaches: * For loop: An explicit loop using a `for` statement, where each iteration is performed explicitly. * For...of loop: A concise way to iterate over arrays and other iterables using a `for...of` statement. * Array#forEach: A built-in method for iterating over an array. **Pros and Cons** Here are the pros and cons of each approach: 1. **For loop**: * Pros: Explicit control, no overhead from built-in methods. * Cons: More verbose, slower due to explicit iteration. 2. **For...of loop**: * Pros: Concise, easier to read, faster than explicit loops. * Cons: Limited control over iteration, may not work with non-array iterables. 3. **Array#forEach**: * Pros: Fast, convenient, works with most array-like objects. * Cons: May have overhead due to built-in method invocation. **Library and Purpose** None of the benchmarks use any external libraries or frameworks. However, `Array` is a built-in JavaScript object that provides methods for working with arrays. **Special JS Features/Syntax** The benchmark uses the following special features/syntax: * **For...of loop**: Introduced in ECMAScript 2015 (ES6), this syntax allows iterating over arrays and other iterables using a concise, for-each-like structure. * **Array#forEach**: Built-in method introduced in ECMAScript 2009, providing a convenient way to iterate over arrays. **Other Alternatives** If you're interested in exploring alternative approaches to iteration, consider the following: 1. **While loop**: An explicit loop using a `while` statement, offering more control than `for` loops but potentially slower. 2. **Closures**: Using functions as iterables or closures to encapsulate state, which can be useful for more complex iterations. 3. **Generators and iterators**: Techniques for creating iterative functions that produce values one at a time, often used in libraries like Lodash. Keep in mind that the choice of iteration approach depends on your specific use case, performance requirements, and personal preference.
Related benchmarks:
Lodash.js vs Native isArrary
Reading array length inside vs outside for loop
Lodash.js vs Native MAGIC
empty an array in JavaScript - splice vs setting length. 444 333
For Loop Leng Inside and Outside
Comments
Confirm delete:
Do you really want to delete benchmark?