Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set for entries vs forEach
(version: 0)
Benchmark the performance diff between sets forEach and for loop over set.entries
Comparing performance of:
Set.forEach vs for x of Set
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const data = new Set for (let i = 0; i < 1000000; i++) { data.add(i) } window.data = data
Tests:
Set.forEach
let sum = 0 window.data.forEach((v) => { sum += v }) console.log(sum)
for x of Set
let sum = 0 for (const v of window.data) { sum += v } console.log(sum)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set.forEach
for x of Set
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/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set.forEach
99.8 Ops/sec
for x of Set
180.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. The provided JSON represents two benchmark definitions for testing the performance difference between using `forEach` and a traditional `for` loop with a `Set` data structure in JavaScript. **Benchmark Definitions:** 1. **Set.entries**: This benchmark definition tests the performance of iterating over a `Set` object using its `entries()` method, which returns an iterator over the set's elements. 2. **for...of Set**: This benchmark definition tests the performance of using a traditional `for...of` loop to iterate over the elements of a `Set` object. **Options Compared:** The two benchmark definitions compare the performance of these two approaches: * `forEach`: A method on the Array prototype that allows iterating over an array-like object or a set of values. * Traditional `for` loop with `Set.entries()`: This approach uses the `entries()` method to get an iterator over the set's elements and then uses a traditional `for` loop to iterate over them. **Pros and Cons:** 1. **forEach**: * Pros: Easy to read and write, concise syntax. * Cons: May incur additional overhead due to the creation of an array-like object or the conversion of the set to an array. 2. Traditional `for...of Set`: * Pros: Directly iterates over the set's elements without creating an intermediate data structure, potentially more efficient. * Cons: Requires a separate iterator function (in this case, `Set.entries()`), which may be slightly less readable. **Library and Purpose:** There is no specific library mentioned in these benchmark definitions. However, it's worth noting that JavaScript sets are implemented using the V8 engine's internal data structures, which provide an efficient way to store and iterate over unique values. **Special JS Features/Syntax:** None of the provided benchmark definitions use any special JavaScript features or syntax beyond what is standard for modern JavaScript (ES6+). The `for...of` loop with a set iterator is a relatively simple construct that leverages built-in JavaScript functionality. **Other Alternatives:** If you're interested in exploring alternative approaches, here are some options: * Using other data structures, such as arrays or linked lists, to iterate over the elements. * Implementing a custom iterator function for sets, potentially providing better performance characteristics than `Set.entries()`. * Utilizing Web Workers or other parallel processing techniques to execute the benchmarking code in multiple threads. Keep in mind that these alternatives may require significant modifications to the benchmark code and might not necessarily provide improved performance results. In summary, the two benchmark definitions on MeasureThat.net compare the performance of iterating over a `Set` object using its `entries()` method versus a traditional `for...of` loop. While both approaches have their pros and cons, the `forEach` method is generally easier to read and write, while the traditional `for...of Set` approach may be more efficient due to direct iteration over the set's elements without creating an intermediate data structure.
Related benchmarks:
Array fill foreach, vs for i loop
loop over Set conditionally vs non-conditionally
set vs array iteration 100k elements
3set vs array iteration New doge333
Comments
Confirm delete:
Do you really want to delete benchmark?