Map vs Array - Collection functions (v2)

Revision 2 of this benchmark created on


Description

Testing collection needs on Map - how does it compare to regular array on find, filter and map?

Setup

var map = new Map();
var rec = {};
var arr = [];
for (var i=0; i< 20; i++) {
	var num = Math.random() * 1000;
	var idd = String(i)+num;
	var obj = {id: idd, a: i !== 8000 ? false : true, coinFlip:num > 500}
	arr.push({...obj})
	map.set(idd, {...obj});
}
var findVal = arr[10].id;
var firstVal = arr[0].id;

Test runner

Ready to run.

Testing in
TestOps/sec
Array map
var nn = arr.map(d => ({b: d.id}));
ready
Map map
var mm = new Array(map.size);
var d;
var c = 0;
for (d of map.values()) {
	mm[c] = {b: d.id};
	c++;
}

ready
Array filter
var nn = arr.filter(d => d.coinFlip);
ready
Map filter
var mm = [];
var d;
for (d of map.values()) {
	if (d.coinFlip) mm.push(d)
}
ready
Array find
var found = arr.find(d => d.a);
ready
Map find
var found
var d;
for (d of map.values()) {
	if (d.a) {
		return d;
	}
}
ready

Revisions

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