Find() vs Get() (v2)

Revision 2 of this benchmark created on


Description

Get seems to be more performant in cases smaller than 100 matching 100. But it switches over to Find when matching 100 against 1000

Setup

const legends = Array.from({ length: 100 }, (_, i) => ({ name: `legend${i}`, visible: Math.random() < 0.5 }));
// const metaLegends =[ { name: `legend${77}`, visible: Math.random() < 0.5}, { name: `legend${88}`, visible: Math.random() < 0.5}, { name: `legend${99}`, visible: Math.random() < 0.5} ];
const metaLegends = Array.from({ length: 100 }, (_, i) => ({ name: `legend${i}`, visible: Math.random() < 0.5 })).sort(() => Math.random() - 0.5);
const newLegends = [];

Test runner

Ready to run.

Testing in
TestOps/sec
.find()
Object.entries(legends).forEach(([name, visible]) => {
	const metaLegend = legends.find((legend) => legend.name === name);
	const legend = metaLegend ?? { name, visible };
	newLegends.push(legend);
})
ready
.get()
const metaLegendsMap = new Map(metaLegends);

Object.entries(legends).forEach(([name, visible]) => {
	const legend = metaLegendsMap.get(name) ?? { name, visible };
	newLegends.push(legend);
});
ready

Revisions

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