Spread Operator Profiling

Benchmark created on


Setup

// Generates random strings
const rnd = (() => {
    const gen = (min, max) => max++ && [...Array(max-min)].map((s, i) => String.fromCharCode(min+i));

    const sets = {
        num: gen(48,57),
        alphaLower: gen(97,122),
        alphaUpper: gen(65,90),
        special: [...`~!@#$%^&*()_+-=[]\{}|;:'",./<>?`]
    };

    function* iter(len, set) {
        if (set.length < 1) set = Object.values(sets).flat(); 
        for (let i = 0; i < len; i++) yield set[Math.random() * set.length|0]
    }

    return Object.assign(((len, ...set) => [...iter(len, set.flat())].join('')), sets);
})();

const generateOrgLikeObj = () => {
	const result = {}
	for (let i = 0; i < 40; i++) {
		result[i] = rnd(1000)
	}
	return result
}

// Create two org-like objects (flat, 40 keys, 1000 bytes per key)
const oldObj = generateOrgLikeObj()
const newObj = generateOrgLikeObj()

Test runner

Ready to run.

Testing in
TestOps/sec
Assign
// Approximate result of updating w/ assign for 1000 orgs
for (let i = 0; i < 1000; i++) {
	const result = newObj
}
ready
Spread
// Approximate result of updating w/ spread for 1000 orgs
for (let i = 0; i < 1000; i++) {
	const result = {... oldObj, ...newObj}
}
ready

Revisions

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