Object spread vs. Object.assign (v2)

Revision 2 of this benchmark created on


Setup

const defaultProperties = {
	'property 0': 'zero',
	'property 1': 'one',
	'property 2': 'two',
	'property 3': 'three',
	'property 4': 'four',
	'property 5': 'five',
	'property 6': 'six',
	'property 7': 'seven',
	'property 8': 'eight',
	'property 9': 'nine',
}

const objects = Array.from(new Array(1000)).map((r, i) => ({
    id: `id-${i}`,
    val: i
}));

Test runner

Ready to run.

Testing in
TestOps/sec
Object spread
const result1 = objects.reduce((acc, curr) => {
	acc[curr.id] = {
		...defaultProperties,
		val: curr.val
	}
	return acc;
}, {});
ready
Object.assign
const result2 = objects.reduce((acc, curr) => {
	acc[curr.id] = Object.assign({}, defaultProperties, {
		val: curr.val
	});
	return acc;
}, {});
ready

Revisions

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