Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Iterators
(version: 0)
Comparing performance of:
Iterator -> Spread vs Iterator --> Spread --> for vs Iterator --> Spread --> forEach
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
Iterator -> Spread
const set = new Set([1, 2, 3]); const iter = set[Symbol.iterator](); let num; while (!(num = iter.next()).done) { console.log(num.value); }
Iterator --> Spread --> for
const set = new Set([1, 2, 3]); const nums = [...set[Symbol.iterator]()]; for (let i = 0; i < nums.size; i++) { console.log(nums[i]); }
Iterator --> Spread --> forEach
const set = new Set([1, 2, 3]); const nums = [...set[Symbol.iterator]()]; nums.forEach(num => { console.log(num); })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Iterator -> Spread
Iterator --> Spread --> for
Iterator --> Spread --> 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 break down the provided benchmark and explain what's being tested, compared options, pros and cons of those approaches, and other considerations. **What is tested?** The benchmark measures the performance of three different ways to iterate over an array created from a Set object in JavaScript. The test cases: 1. `Iterator -> Spread` 2. `Iterator --> Spread --> for` (using a `for` loop) 3. `Iterator --> Spread --> forEach` (using the `forEach` method) **Options compared** The three options being compared are: * Using the spread operator (`[...set[Symbol.iterator]()]`) to create an array from the iterator * Using a `for` loop to iterate over the array * Using the `forEach` method to iterate over the array **Pros and Cons of each approach:** 1. **Iterator -> Spread**: * Pros: concise, efficient, and easy to read. * Cons: may not be as readable for those unfamiliar with JavaScript's iterator protocol. 2. **Iterator --> Spread --> for**: * Pros: allows for more control over the iteration process (e.g., breaking out of the loop). * Cons: more verbose and less efficient than the `for` loop approach. 3. **Iterator --> Spread --> forEach**: * Pros: concise and easy to read, similar to the `for` loop approach but with less boilerplate code. * Cons: may not be as efficient as a pure `for` loop. **Other considerations:** * The benchmark uses a `Set` object to create an array of unique elements. This is done to test how the iterator protocol handles iteration over sets. * The benchmark does not consider other factors that might affect performance, such as the size of the input set or the specific hardware and software configuration used. **Library use:** None of the provided benchmark code uses any external libraries. **Special JS features or syntax:** The benchmark code uses: * `Symbol.iterator`: a built-in JavaScript symbol that allows an object to be iterated over using its own iterator protocol. * The spread operator (`[...set[Symbol.iterator]()]`): a feature introduced in ECMAScript 2015 (ES6). Overall, the benchmark provides insight into how different iteration methods perform in practice, and can help developers choose the most efficient approach for their specific use case.
Related benchmarks:
Spread performance
Iterator with Array#pop() vs iterator with Array#shift()
Iterator vs Index for loop
iterators vs callbacks
Comments
Confirm delete:
Do you really want to delete benchmark?