Testing native Set speed vs JS iteration

Benchmark created on


Description

Benchmarking (mostly for a Chrome bug)

Setup

let arr = new Array(100)
for (let i = 0; i < 100; i++) {
  arr[i] = 'key' + i
}
let set = new Set(arr)

function assertEqual(a, b) {
	if (a.size !== b.size) {
		throw new Error("Didn't clone properly'")
	}
}

Test runner

Ready to run.

Testing in
TestOps/sec
Clone from Set constructor
let clone = new Set(set);
assertEqual(set, clone);
ready
Clone from constructor (iterator)
let clone = new Set([...set]);

assertEqual(set, clone);
ready
Clone from JS iteration
let clone = new Set();
for (let item of set) {
	clone.add(item);
}

assertEqual(set, clone);
ready
Clone from intersection
let clone = set.intersection(set);

assertEqual(set, clone);
ready
Clone from difference
let clone = set.difference(new Set());

assertEqual(set, clone);
ready
Clone from union
let clone = set.union(new Set());

assertEqual(set, clone);
ready
Clone from JS forEach
let clone = new Set();
set.forEach(item => clone.add(item));

assertEqual(set, clone);
ready

Revisions

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