reduce vs filter (v4)

Revision 4 of this benchmark created on


Description

reduce vs filter on array

Setup

function makeid() {
    var result           = '';
    var characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var charactersLength = characters.length;
    for ( var i = 0; i < 10; i++ ) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    return result;
}
let arr = [];
for (let i = 0; i < 1000; i++) {
	arr.push(makeid())
}

Test runner

Ready to run.

Testing in
TestOps/sec
reduce
arr.reduce((acc, type) => {
	if (type.startsWith('a')) acc.push(type);
	return acc;
}, []);
ready
filter
arr.filter((type) => type.startsWith('a'))
ready

Revisions

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