Linear sort vs. Index

Benchmark created on


Setup

const items = Array.from(
  {length: 200},
  (item, index) => index
);

const selections = Array.from(
  {length: 10}, 
  () => items[Math.floor(Math.random()*items.length)]
);

Test runner

Ready to run.

Testing in
TestOps/sec
linear lookup

const ordered = items.reduce((acc, child) => {
	const found = selections.find((item) => item === child)
	return found ? [
		...acc,
		found		
	] : acc;
}, [])
ready
Map lookup

const indexMap = items.reduce((acc, ch, index) => ({...acc, [ch]: index}));

const ordered = selections.sort((a, b) => (indexMap[a] ?? items.length) - (indexMap[b] ?? items.length))
ready

Revisions

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