const obj1 = { a: "a", b: "b", c: "c", d: "d", e: "e", f: "f", g: "g", h: "h", i: "i" };
const obj2 = { b: "b", j: "j", k: "k", l: "l", m: "m", n: "n", o: "o", p: "p", q: "q" };
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
keys.push.apply(keys, symbols);
}
return keys;
}
function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
if (i % 2) {
ownKeys(Object(source), true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
}
return target;
}
window.obj1 = obj1;
window.obj2 = obj2;
window._objectSpread2 = _objectSpread2;
const finalObject = {
obj1,
obj2
};
const finalObject = Object.assign({}, obj1, obj2);
_objectSpread2(obj1, obj2);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Using the spread operator | |
Using Object.assign | |
Babel |
Test name | Executions per second |
---|---|
Using the spread operator | 3916761.2 Ops/sec |
Using Object.assign | 3098604.5 Ops/sec |
Babel | 527768.5 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
Benchmark Overview
The benchmark compares the performance of three different approaches to merge two objects: the spread operator (...
), Object.assign
, and Babel's _objectSpread2
function.
Options Compared
...
): This is a modern JavaScript feature that allows you to expand an object into a new object using the syntax const obj1 = { ...obj2 }
. It was introduced in ECMAScript 2018.Object
prototype that merges two or more source objects into a target object._objectSpread2
function: This is a helper function provided by Babel, a popular JavaScript transpiler, to enable the spread operator feature in older browsers and environments.Pros and Cons
...
)_objectSpread2
functionLibrary Used
In this benchmark, Babel is used as a transpiler to enable the spread operator feature. The _objectSpread2
function is part of the Babel code generation.
Special JS Feature or Syntax
The spread operator (...
) is a new JavaScript feature introduced in ECMAScript 2018. It allows you to expand an object into a new object using the syntax const obj1 = { ...obj2 }
.
Alternatives
If the spread operator is not supported by your target browser or environment, you can use other approaches:
Object.assign
and iteration (e.g., Object.keys(obj1).forEach(key => obj1[key] = obj2[key])
)In summary, the benchmark compares the performance of three approaches to merge two objects: the spread operator (...
), Object.assign
, and Babel's _objectSpread2
function. The choice of approach depends on the target browser or environment, as well as personal preference for syntax and readability.