"use strict";
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 _objectSpread(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; }
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; }
var firstObject = {
sampleData: 'Hello world'
};
var secondObject = {
moreData: 'foo bar'
};
var finalObject = _objectSpread(_objectSpread({}, firstObject), secondObject);
const firstObject = { sampleData: 'Hello world' }
const secondObject = { moreData: 'foo bar' }
const finalObject = Object.assign({}, firstObject, secondObject);
const firstObject = { sampleData: 'Hello world' }
const secondObject = { moreData: 'foo bar' }
const finalObject = {firstObject, secondObject}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Using the spread operator | |
Using Object.assign | |
use... |
Test name | Executions per second |
---|---|
Using the spread operator | 711281.1 Ops/sec |
Using Object.assign | 5268026.0 Ops/sec |
use... | 20779810.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
The provided benchmark compares three different ways to merge two objects in JavaScript: using the spread operator, Object.assign()
, and native spread (introduced in ECMAScript 2018). We'll break down each approach, its pros and cons, and consider other alternatives.
Using the Spread Operator (...
)
var finalObject = _objectSpread(_objectSpread({}, firstObject), secondObject);
The spread operator is a concise way to merge objects. It's defined in the _objectSpread()
function, which recursively merges source objects into a target object.
Pros:
Cons:
Using Object.assign()
const finalObject = Object.assign({}, firstObject, secondObject);
Object.assign()
is a built-in method that merges one or more source objects into a target object. It's defined on the Object.prototype
.
Pros:
Cons:
Native Spread (ECMAScript 2018)
const finalObject = {...firstObject, ...secondObject};
Native spread is a more recent addition to JavaScript, introduced in ECMAScript 2018. It's defined on the Object.prototype
and uses native array operations under the hood.
Pros:
Object.assign()
.Cons: None significant.
Now, let's consider other alternatives to merge objects in JavaScript:
Object.fromEntries()
. This approach is not recommended, as it has performance overhead and may not preserve object properties.In conclusion, the spread operator, Object.assign()
, and native spread are all efficient ways to merge objects in JavaScript. The choice of which one to use depends on your specific requirements, including performance concerns, syntax preference, and compatibility with older browsers.