Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Object assign vs direct assignment by iterating keys and assigning
Should you be saving your fields to an object with Object.keys(toSave).forEach(k => ... or by Object.assign(destination, toSave);
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser:
Chrome 124
Operating system:
Windows
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
With Object.assign()
2186395.0 Ops/sec
With Iteration and Assignment
4062657.0 Ops/sec
With Iteration and .keys().forEach
3414393.5 Ops/sec
Tests:
With Object.assign()
var toSave = { "def":100101102, "ghi":null, "jkl":106107108, "mno":109110111.106107108, "pqr":'112113114', "stu":NaN, "vwx":118119120, "yz{":[ { "vwx":118119120 }, { "vwx":118119120 } ], }; var dest = { "abc":979899 }; if (toSave) { Object.assign(dest, toSave); }
With Iteration and Assignment
var toSave = { "def":100101102, "ghi":null, "jkl":106107108, "mno":109110111.106107108, "pqr":'112113114', "stu":NaN, "vwx":118119120, "yz{":[ { "vwx":118119120 }, { "vwx":118119120 } ], }; var dest = { "abc":979899 }; if (toSave) { var keys = Object.keys(dest); for (var i = 0; i < keys.length; i++) { dest[keys[i]] = toSave[keys[i]]; } }
With Iteration and .keys().forEach
var toSave = { "def":100101102, "ghi":null, "jkl":106107108, "mno":109110111.106107108, "pqr":'112113114', "stu":NaN, "vwx":118119120, "yz{":[ { "vwx":118119120 }, { "vwx":118119120 } ], }; var dest = { "abc":979899 }; if (toSave) { var keys = Object.keys(dest).forEach(key => { dest[key] = toSave[key]; }); }