arrays vs. collections

Benchmark created on


Setup

const arrayData = new Array(1000).fill(0).map(() => Math.random());


const mapData = new Map(arrayData.map((v, i) => (["item" + i, v])));
const setData = new Set(arrayData);

Test runner

Ready to run.

Testing in
TestOps/sec
legacy for loop w/ array
let sum = 0;

for ( let i = 0; i < arrayData.length; i++ ) {
	sum += arrayData[i];
}
ready
for of loop over map
let sum = 0;

for ( const entry of mapData ) {
	sum += entry[1];
}
ready
for of loop over map keys
let sum = 0;

for ( const key of mapData.keys() ) {
	sum += mapData.get( key );
}
ready
for of loop over set
let sum = 0;

for ( const entry of setData ) {
	sum += entry[1];
}
ready

Revisions

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