array.filter vs for

Benchmark created on


Setup

function generateRandomObjects(num) {
    const randomObjects = [];

    for (let i = 0; i < num; i++) {
        randomObjects.push({
            id: i,
            value: Math.random() // Случайное число между 0 и 1
        });
    }

    return randomObjects;
}

const arrayOfRandomObjects = generateRandomObjects(300);

Test runner

Ready to run.

Testing in
TestOps/sec
array.filter
const result = arrayOfRandomObjects.filter(item => item.value <= 0.5)
ready
for
const result = [];
for (let i = 0; i < arrayOfRandomObjects.length; i++)
{
	if (arrayOfRandomObjects[i].value <= 0.5)
	   result.push(arrayOfRandomObjects[i]);
}
ready

Revisions

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