Init array with `reduce`

Benchmark created on


Setup

const allData = ['A-1', 'A-2', 'A-3', , 'A-4', 'B-1', 'B-2', 'B-3', 'B-4', 'B-5', 'B-6'];
const partData = ['B-1', 'B-2', 'B-3', 'B-4', 'B-5', 'B-6'];

Test runner

Ready to run.

Testing in
TestOps/sec
Use reduce
const existingPart = partData.reduce((acc, curr) => Object.assign(acc, {[curr]: true}), {});

const notInPart = allData.filter(v => !existingPart[v]);
ready
Use Set
const existingPart = new Set(partData);

const notInPart = allData.filter(v => !existingPart.has(v));
ready
Use Map + has
const existingPart = new Map(partData.map(v => [v, true]));

const notInPart = allData.filter(v => !existingPart.has(v));
ready
Use Map + get
const existingPart = new Map(partData.map(v => [v, true]));

const notInPart = allData.filter(v => !existingPart.get(v));
ready

Revisions

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