const sourceObjects = [];
const destObjects = [];
for (let i = 0; i < 500; ++i) {
const sourceObject = {};
for (let j = 0; j < 10; ++j) {
sourceObject[Math.random().toFixed(0) * 1000] = Math.random().toFixed(0) * 1000;
}
const destObject = {};
for (let j = 0; j < 5; ++j) {
destObject[Math.random().toFixed(0) * 1000] = Math.random().toFixed(0) * 1000;
}
sourceObjects.push(sourceObject);
destObjects.push(destObject);
}
window.sourceObjects = sourceObjects;
window.destObjects = destObjects;
const sourceObjects = window.sourceObjects;
const destObjects = window.destObjects;
for (let i = 0; i < sourceObjects.length; ++i) {
const destObject = destObjects[i];
const sourceObject = sourceObjects[i];
const newObject = Object.assign(destObject, sourceObject);
}
function objForEachKey(target, callbackfn) {
if (target) {
for (var prop in target) {
if (Object.prototype.hasOwnProperty.call(target, prop)) {
callbackfn.call(target, prop, target[prop]);
}
}
}
}
function extend(obj, obj2, obj3, obj4, obj5) {
// Variables
var extended = {};
var deep = false;
var i = 0;
var length = arguments.length;
var objProto = Object.prototype;
var theArgs = arguments;
// Check if a deep merge
if (objProto.toString.call(theArgs[0]) === "[object Boolean]") {
deep = theArgs[0];
i++;
}
// Loop through each object and conduct a merge
for (; i < length; i++) {
var obj = theArgs[i];
objForEachKey(obj, function (prop, value) {
extended[prop] = value;
});
}
return extended;
}
const sourceObjects = window.sourceObjects;
const destObjects = window.destObjects;
for (let i = 0; i < sourceObjects.length; ++i) {
const destObject = destObjects[i];
const sourceObject = sourceObjects[i];
const newObject = extend(destObject, sourceObject);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.assign | |
Polyfill Extend |
Test name | Executions per second |
---|---|
Object.assign | 1333.7 Ops/sec |
Polyfill Extend | 524.6 Ops/sec |
Let's dive into the explanation of the provided benchmark.
What is tested?
The benchmark compares two approaches to merge objects in JavaScript:
Object.assign()
: A built-in method that copies properties from one or more source objects to a target object.Options compared:
Object.assign()
vs. the Polyfill Extend implementation.Pros and Cons:
Library/Extension:
In this benchmark, there is no explicit library or extension being tested. However, the Polyfill Extend implementation uses a custom objForEachKey()
function and an extend()
function from the Lodash library (not explicitly mentioned in the benchmark code). The use of Lodash suggests that the test author wanted to compare the performance of Object.assign()
against a well-known polyfill implementation.
Special JS feature or syntax:
There are no special JavaScript features or syntax being tested in this benchmark. Both approaches rely on standard JavaScript constructs.
Other alternatives:
If you're interested in exploring alternative merge methods, here are a few:
_.merge()
: A built-in method that merges two objects into one.Object.assign()
with spread syntax: An updated implementation of Object.assign()
using the spread operator ({ ... }
).In conclusion, this benchmark provides a useful comparison between two approaches to merging objects in JavaScript. By choosing one or both of these methods, developers can optimize their code for performance and readability.