defineProperties vs for in

Benchmark created on


Setup

function fn() {}

for (let i = 0; i < 10000; i++) {
    const key = `key${i}`; // Simple sequential key
    const value = Math.random(); // Random number as value
    fn[key] = value;
}

Test runner

Ready to run.

Testing in
TestOps/sec
defineProperties
const clonedFn = fn.bind(null);

Object.defineProperties(clonedFn, Object.getOwnPropertyDescriptors(fn));
ready
for in
const clonedFn = fn.bind(null);

for (const k in fn) {
	clonedFn[k] = fn[k];
}
ready

Revisions

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