Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array iteration SMALL3
(version: 9)
Comparing performance of:
for element of SMALL vs for const element of SMALL vs myArr forEach SMALL vs array index SMALL vs map vs forEach ES6 SMALL
Created:
7 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var myArraySmall = Array(3); for (let i=0; i<3; i++) { myArraySmall[i] = i; }
Tests:
for element of SMALL
for (ele of myArraySmall) { let something = ele; }
for const element of SMALL
for (const ele of myArraySmall) { let something = ele; }
myArr forEach SMALL
myArraySmall.forEach(function(s) { let something = s; });
array index SMALL
for (let i=0; i<3; i++) { let something = myArraySmall[i]; }
map
myArraySmall.map((i) => { let something = i; });
forEach ES6 SMALL
myArraySmall.forEach(s => { let something = s; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
for element of SMALL
for const element of SMALL
myArr forEach SMALL
array index SMALL
map
forEach ES6 SMALL
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.1:latest
, generated one year ago):
Let's break down the benchmark test cases and results. **Benchmark Description** The benchmark measures the performance of different ways to iterate over an array in JavaScript. The test case creates an array `myArraySmall` with three elements, each initialized with a value from 0 to 2. Then, six different methods are tested for iterating over this array: 1. Using a `for...of` loop with a constant variable declaration (`let something = ele;`) 2. Using a `for...of` loop with a mutable variable declaration (`let something = ele;`) 3. Using the `forEach()` method with a traditional callback function 4. Using an indexed `for` loop to access elements by their indices (`myArraySmall[i]`) 5. Using the `map()` method to create a new array of transformed values (not executed in this case, but shown for completeness) 6. Using the modern `forEach()` method with an arrow function (introduced in ES6) **Test Results** The results show the number of executions per second for each test case on a specific browser and device platform (Firefox 66 on Windows 7 Desktop). The faster method is expected to have a higher execution rate. Here are the key takeaways from the results: 1. **Using `forEach()` with an arrow function** (`myArr forEach SMALL` and `forEach ES6 SMALL`) is the fastest approach, outperforming other methods by up to 2.9x (for Firefox 66 on Windows 7 Desktop). 2. **Indexed `for` loop** (`array index SMALL`) performs well, but its execution rate is lower than the `forEach()` method. 3. **Using `map()`** (not executed in this case) might be useful for creating new arrays, but it's slower than other methods for simple iteration tasks. **Options Compared** The benchmark compares six different ways to iterate over an array: 1. `for...of` loop with a constant variable declaration 2. `for...of` loop with a mutable variable declaration 3. Indexed `for` loop 4. Traditional `forEach()` method 5. Modern `forEach()` method (ES6) with an arrow function 6. `map()` method (not executed in this case) **Pros and Cons of Different Approaches** Each approach has its pros and cons: 1. **For...of loop**: Simple to read, but might be slower than other methods. * Pros: Easy to write, suitable for small arrays. * Cons: Performance might suffer for large arrays. 2. **Indexed `for` loop**: A traditional way to access elements by their indices. * Pros: Suitable for indexed operations. * Cons: Not as concise as modern iteration methods. 3. **forEach() method** (traditional and ES6): Allows for easy iteration over arrays without manual indexing. * Pros: Easy to read, suitable for most use cases. * Cons: Might be slower than `for...of` loops or indexed `for` loops. **Library Usage** In this benchmark, the only library used is the built-in JavaScript array methods (e.g., `forEach()`, `map()`). **Special JS Feature or Syntax** The modern `forEach()` method and arrow functions are special features introduced in ES6. They improve code readability and conciseness. **Alternatives** Other alternatives for iterating over arrays include: 1. **Array iterators**: You can use array iterators like `array[Symbol.iterator]()` to iterate over arrays. 2. **Loops with incrementing indices**: While not the most efficient way, using a loop with an incremented index (e.g., `for (let i = 0; i < array.length; i++)`) is still a viable option. Keep in mind that this benchmark focuses on iterating over small arrays. For larger datasets or specific use cases, other approaches might be more suitable.
Related benchmarks:
array size 7
feachand
array vs int32array3
Test array and unshift
Comments
Confirm delete:
Do you really want to delete benchmark?