Filter vs Set removal

Benchmark created on


Description

Is it faster to filter an array of ids OR to convert the array to a set then remove from the set

Setup

const items = new Array(20000).fill(0).map(() => Math.random());

const itemToRemove = items[Math.floor(Math.random() * 20000)];

Test runner

Ready to run.

Testing in
TestOps/sec
Filter an array
const itemsCopy = [...items];

itemsCopy.filter(item => item !== itemToRemove);
ready
Conver to set, remove, then convert back
const itemsCopy = [...items];

const tmpSet = new Set(itemsCopy);
tmpSet.delete(itemToRemove);
[...tmpSet];
ready

Revisions

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