Sort a value first: sort vs filter

Benchmark created on


Setup

let array = [];
const arraySize = 10000;

for (let i = 0; i < arraySize; i++) {
	array.push({
		libelle: i%(arraySize/100)
	});
}

const valueFirst = 5;

Test runner

Ready to run.

Testing in
TestOps/sec
sort
array.sort((a, b) => (b.libelle === valueFirst) - (a.libelle === valueFirst));
ready
filter
array = [
	...array.filter(v => v.libelle === valueFirst),
	...array.filter(v => v.libelle !== valueFirst)
];
ready

Revisions

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