Find() vs Get()

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.map((legend) => [legend.name, legend.visible]));

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.