Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array for vs Array for of vs Set for of vs Set forEach
(version: 0)
Testing the difference between native loops and find()
Comparing performance of:
Array for-loop vs Array for..of vs Set for of vs Set forEach
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id='test'></div>
Script Preparation code:
window.fn = function fn() {} window.arr = new Array(10).fill(0).map((x, i) => i.toString()); window.set = new Set(arr);
Tests:
Array for-loop
for(i=0, l = arr.length; i<l; i++){ fn(arr[i]); }
Array for..of
for (let value of arr) { fn(value); }
Set for of
for (let value of set) { fn(value); }
Set forEach
set.forEach(fn);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Array for-loop
Array for..of
Set for of
Set 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. **Benchmark Overview** The provided benchmark tests four different approaches to iterate over arrays and sets in JavaScript: `for` loops, `for...of`, `forEach`, and using the `Set` data structure. **Script Preparation Code** The script preparation code initializes two variables: 1. `window.arr`: an array of 10 zeros, created using the `Array` constructor and `fill()` method. 2. `window.set`: a new `Set` object initialized with the same elements as `arr`. **Html Preparation Code** The HTML preparation code creates a simple `<div>` element with an ID of "test". **Individual Test Cases** Each test case defines a specific benchmarking scenario using JavaScript code. The cases are: 1. **Array for-loop**: uses a traditional `for` loop to iterate over the `arr` array. 2. **Array for...of**: uses the `for...of` loop syntax to iterate over the `arr` array. 3. **Set for of**: attempts to use the `for...of` loop syntax with a `Set` object, which is not supported in JavaScript. 4. **Set forEach**: uses the `forEach()` method on the `set` Set object to iterate over its elements. **Pros and Cons** Here's a brief analysis of each approach: 1. **Array for-loop**: traditional way of iterating over arrays, straightforward to understand and implement. However, it may not be as efficient as other approaches. 2. **Array for...of**: introduced in ECMAScript 2015 (ES6), this syntax provides a concise and expressive way to iterate over arrays. It's generally faster than the traditional `for` loop approach. 3. **Set for of**: **not supported** in JavaScript, which means this test case is intentionally skipped or incorrect. 4. **Set forEach**: uses the `forEach()` method on the Set object, which is a standard way to iterate over its elements. This approach is likely to be fast and efficient. **Library Used** In none of the provided test cases does any library is used apart from the default browser's JavaScript engine. **Special JS Features or Syntax** None of the provided test cases use special JavaScript features or syntax, such as async/await or Promises. **Other Alternatives** If you'd like to explore alternative ways to measure performance in your benchmarking setup, consider using: 1. **Web Workers**: run code in separate threads to improve parallelism and concurrency. 2. **Benchmark libraries**: specialized libraries like Benchmark.js, Microbenchmark, or jsperf provide more comprehensive features for measuring performance. Keep in mind that the choice of alternative approaches depends on your specific use case and requirements.
Related benchmarks:
Array fill foreach, vs for i loop
for of vs forEach with console log
set vs some
set vs some 1000000
Array fill map, vs for i loop
Comments
Confirm delete:
Do you really want to delete benchmark?