Set vs Map (v4)

Revision 4 of this benchmark created on


Preparation HTML

	

Setup

const data_128 =
	Array(2 ** 14)
		.fill(0)
		.map((_, i) => i % 2 ** 7);
		
const data_4096 =
	Array(2 ** 14)
		.fill(0)
		.map((_, i) => i % 2 ** 12);

const data_128_ref =
	Array(2 ** 7)
		.fill(0)
		.map((_, i) => i);
		
const data_4096_ref =
	Array(2 ** 12)
		.fill(0)
		.map((_, i) => i);

Test runner

Ready to run.

Testing in
TestOps/sec
Map 128
const xy = new Map();

data_128.forEach(v => xy.set(v, true));

const unique_items = Array.from(xy.keys());

if (unique_items.length !== data_128_ref.length) {
	throw new Error();
}

for (let i = 0; i <= data_128_ref.length; ++i) {
	if (data_128_ref[i] !== unique_items[i]) {
		throw new Error();
	}
}
ready
Set 128
const sxy = new Set();

data_128.forEach(v => sxy.add(v));

const unique_items = Array.from(sxy.values());

if (unique_items.length !== data_128_ref.length) {
	throw new Error();
}

for (let i = 0; i <= data_128_ref.length; ++i) {
	if (data_128_ref[i] !== unique_items[i]) {
		throw new Error();
	}
}
ready
Map 4096
const xy = new Map();

data_4096.forEach(v => xy.set(v, true));

const unique_items = Array.from(xy.keys());

if (unique_items.length !== data_4096_ref.length) {
	throw new Error();
}

for (let i = 0; i <= data_4096_ref.length; ++i) {
	if (data_4096_ref[i] !== unique_items[i]) {
		throw new Error();
	}
}
ready
Set 4096
const sxy = new Set();

data_4096.forEach(v => sxy.add(v));

const unique_items = Array.from(sxy.values());

if (unique_items.length !== data_4096_ref.length) {
	throw new Error();
}

for (let i = 0; i <= data_4096_ref.length; ++i) {
	if (data_4096_ref[i] !== unique_items[i]) {
		throw new Error();
	}
}
ready

Revisions

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