Reduce or filter + map (v2)

Revision 2 of this benchmark created on


Setup

const users = [];

for (let i = 0; i < 10000; i++) {
  const user = {};
  const randInt = Math.round(Math.random() * 10);
  user.name = String.fromCharCode(randInt + 65) + String.fromCharCode(Math.ceil(i / 2));
  user.active = randInt%2 === 0;
  users.push(user)
}

Test runner

Ready to run.

Testing in
TestOps/sec
Reduce
const activeUserNames = users.reduce((acc, user) => {
	if (user.active) {
		acc.push(user.name)
	}
	return acc;
},[]);
ready
Reduce Right
const activeUserNames = users.reduceRight((acc, user) => {
	if (user.active) {
		acc.push(user.name)
	}
	return acc;
},[]);
ready
forEach
let activeUserNames = [];


users.forEach((user) => {
	if (user.active) {
		activeUserNames.push(user);
	}
})
ready
for - in
let activeUserNames = [];


for (let user in users) {
	if (users[user]?.active) {
		activeUserNames.push(user)
	}
}
ready
for - of
let activeUserNames = [];


for (let user of users) {
	if (user.active) {
		activeUserNames.push(user)
	}
}
ready
filter + map
const activeUserNames = users
.filter((user) => user.active)
.map((user) => user.name);
ready

Revisions

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