array.filter vs for (v2)

Revision 2 of this 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).length;
ready
for
let iter = 0;
for (let i = 0; i < arrayOfRandomObjects.length; i++)
{
	if (arrayOfRandomObjects[i].value <= 0.5)
	   iter++;
}
ready
reduce
const result = arrayOfRandomObjects.reduce((accumulator, obj) => {
    return obj.value <= 0.5 ? accumulator + 1 : accumulator;
}, 0);
ready

Revisions

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