reduce vs for-loop vs forEach (v2)

Revision 2 of this benchmark created on


Description

reduce vs for-loop vs forEach on array

Setup

let arr = [];
for (let i = 0; i < 100000; i++) {
	arr.push(new Date(i))
}

Test runner

Ready to run.

Testing in
TestOps/sec
reduce
arr.reduce((big, a) => ({
	...big,
	[a.getTime()]: {
		y: a.getFullYear(),
		M: a.getMonth(),
		D: a.getDate(),
		h: a.getHours(),
		m: a.getMinutes(),
		s: a.getSeconds(),
		ms: a.getMilliseconds()
	}
}), {})
ready
for-loop (map)
let m = new Map()
for (const a of arr) {
	m.set(a.getTime(), {
		y: a.getFullYear(),
		M: a.getMonth(),
		D: a.getDate(),
		h: a.getHours(),
		m: a.getMinutes(),
		s: a.getSeconds(),
		ms: a.getMilliseconds()
	})
}
Object.fromEntries(m)
ready
for-loop (object)
let m = {}
for (const a of arr) {
	m[a.getTime()] = {
		y: a.getFullYear(),
		M: a.getMonth(),
		D: a.getDate(),
		h: a.getHours(),
		m: a.getMinutes(),
		s: a.getSeconds(),
		ms: a.getMilliseconds()
	})
}
ready
forEach (map)
let m = new Map()
arr.forEach(a => {
	m.set(a.getTime(), {
		y: a.getFullYear(),
		M: a.getMonth(),
		D: a.getDate(),
		h: a.getHours(),
		m: a.getMinutes(),
		s: a.getSeconds(),
		ms: a.getMilliseconds()
	})
})
Object.fromEntries(m)
ready
forEach (object)
let m = {}
arr.forEach(a => {
	m[a.getTime()] = {
		y: a.getFullYear(),
		M: a.getMonth(),
		D: a.getDate(),
		h: a.getHours(),
		m: a.getMinutes(),
		s: a.getSeconds(),
		ms: a.getMilliseconds()
	}
})
ready

Revisions

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