Iterating over a set

Benchmark created on


Setup

const hundredKSet = new Set()

for(let i; i < 100_000; i++){
	hundredKSet.add(Math.random())
}

Test runner

Ready to run.

Testing in
TestOps/sec
Using Set.forEach()
let sum = 0
hundredKSet.forEach(el => sum += el)
ready
Using for...of
let sum = 0
for (el of hundredKSet) {
	sum += el
}
ready
Using basic for loop
const iterator = hundredKSet[Symbol.iterator]()
const sum = 0

for(let i; i < hundredKSet.size;i++){
	el = iterator.next().value
	sum += el
}
ready
Using basic for loop over `entries`
const entries = [...hundredKSet]
const sum = 0

for(let i; i < entries.length; i++){
	sum += entries[i]
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.