Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set for-of vs iterator
(version: 0)
Comparing performance of:
for of vs iter
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; while(arr.length < 1000) arr.push(Math.random()*100000); var set = new Set(arr);
Tests:
for of
for(v of set) { let a = v / 2; }
iter
let iter = set.values(); let item = iter.next(); while(!item.done){ let a = item.value / 2; item = iter.next(); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for of
iter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for of
14632.8 Ops/sec
iter
426408.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two approaches to iterate over a Set object: using a traditional for loop with the `of` keyword (also known as "for-of") versus using an iterator manually. The test creates a large array, converts it to a Set, and then iterates over the set using both methods. **Options Compared** The benchmark compares two options: 1. **For-Of Loop**: Uses the `of` keyword to iterate over the Set object in a traditional for loop syntax. 2. **Manual Iterator**: Manually creates an iterator from the Set object and uses its `next()` method to retrieve each value. **Pros and Cons** **For-Of Loop:** Pros: * Readability: The code is easier to read and understand, as it follows a more conventional for loop structure. * Syntax Sugar: The `of` keyword provides a concise way to iterate over a Set object without manually creating an iterator. Cons: * Performance Overhead: Some argue that the `for-of` loop can introduce performance overhead due to the need to call the array's `length` property and create an iterator internally. **Manual Iterator:** Pros: * Control: By manually creating an iterator, you have more control over the iteration process and can optimize it for specific use cases. * Performance: In some cases, manual iteration can be faster than using the `for-of` loop, as it avoids the overhead of creating an iterator internally. Cons: * Complexity: The code requires more lines to manually create and manage the iterator, making it less readable. **Other Considerations** The benchmark assumes that the Set object has a large number of elements (1000 in this case), which may not be representative of real-world scenarios where sets are typically smaller. Additionally, the benchmark focuses on raw performance numbers rather than more nuanced aspects like memory usage or cache efficiency. **Library Usage** In this benchmark, the `Set` object is used as is, without any external libraries. The Set data structure is a built-in JavaScript object that provides efficient membership testing and iteration capabilities. **Special JS Features/Syntax** The benchmark uses the following special features/syntax: * The `of` keyword in the for loop syntax. * The `values()` method to create an iterator from the Set object. * The `next()` method to retrieve each value from the iterator. * The use of a while loop with manual iteration. Note that these features are widely supported and don't require any special knowledge or configuration.
Related benchmarks:
Fill array with random integers
Array .push() vs .unshift() with random numbers
array.from vs spread with set
Array push or set
Comments
Confirm delete:
Do you really want to delete benchmark?