A

Benchmark created on


Setup

const items= [];
for (let i = 0; i < 20_000; i++) {
  items.push({ code: i.toString() });
}

const selected = [];
for (let i = 20_000; i > 0; i--) {
  selected.push(i.toString());
}

Test runner

Ready to run.

Testing in
TestOps/sec
SM
const items2 = items.slice().sort((a, b) => selected.indexOf(a.code) - selected.indexOf(b.code));
ready
MC
const map = new Map(items.map(i => [i.code, i]));

const items2 = selected.map((sl) => map.get(sl)).filter((sl) => sl !== undefined);
ready
SI
const map = new Map(selected.map((s, i) => [s, i]));

const items2 = items.slice().sort((a, b) => (map.get(a) ?? - 1) - (map.get(b) ?? - 1));
ready

Revisions

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