Iterator vs Array

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Array
class TwoKeyMap {
	_data = {};

	set(first, second, value){
		if (!this._data[first]) {
			this._data[first] = {};
		}
		this._data[first][second] = value;
	}

	get(first, second) {
		return this._data[first]?.[second];
	}

	clear() {
		this._data = {};
	}

	values() {
		const result = [];
		for (const first in this._data) {
			for (const second in this._data[first]) {
				const value = this._data[first][second];
				if (value) {
					result.push(value);
				}
			}
		}
		return result;
	}
}

const m = new TwoKeyMap();
for (let i = 0; i < 100; i++) {
	for (let j = 0; j < 100; j++) {
		m.set(i, j, i % j);
	}
}
for (let x = 0; x < 100; x++) {
	const val = m.values();
	for (const x of val) {
		if (x > 10000) { throw new Error(); }
	}
}
ready
Iterator
class TwoKeyMap {
	_data = {};

	set(first, second, value){
		if (!this._data[first]) {
			this._data[first] = {};
		}
		this._data[first][second] = value;
	}

	get(first, second) {
		return this._data[first]?.[second];
	}

	clear() {
		this._data = {};
	}

	*values() {
		for (const first in this._data) {
			for (const second in this._data[first]) {
				const value = this._data[first][second];
				if (value) {
					yield value;
				}
			}
		}
	}
}


const m = new TwoKeyMap();
for (let i = 0; i < 100; i++) {
	for (let j = 0; j < 100; j++) {
		m.set(i, j, i % j);
	}
}
for (let x = 0; x < 100; x++) {
	const val = m.values();
	for (const x of val) {
		if (x > 10000) { throw new Error(); }
	}
}
ready

Revisions

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