Updates:
Labels: Hotlist-NodeJS
Comment #6 on issue 5988 by
bmeu...@chromium.org: Optimize Object.assign
https://bugs.chromium.org/p/v8/issues/detail?id=5988#c6According to
https://github.com/nodejs/node/pull/16081#pullrequestreview-68136151 the hand-rolled Object.keys based util._extend() still beats the shit out of Object.assign even for the simple case, i.e. nothing fancy, just:
================< bench-object-assign.js >==================================
if (typeof console === 'undefined') console = {log:print};
const N = 1e6;
const TESTS = [];
(function() {
const options = {a:1, b:'2', c:null, d:0.1};
function _extend(target, source) {
// Don't do anything if source isn't an object
if (source === null || typeof source !== 'object') return target;
var keys = Object.keys(source);
var i = keys.length;
while (i--) {
target[keys[i]] = source[keys[i]];
}
return target;
}
TESTS.push(
function objectAssignEmpty() {
return Object.assign({}, options);
},
function utilExtendEmpty() {
return _extend({}, options);
});
})();
function test(fn) {
var result;
for (var i = 0; i < N; i += 1) result = fn();
return result;
}
test(x => x);
for (var j = 0; j < TESTS.length; ++j) {
test(TESTS[j]);
}
for (var j = 0; j < TESTS.length; ++j) {
var startTime = Date.now();
test(TESTS[j]);
console.log(TESTS[j].name + ':', (Date.now() - startTime), 'ms.');
}
============================================================================
On V8 ToT this shows a significant performance penalty for using Object.assign:
============================================================================
$ out/Release/d8 bench-object-assign.js
objectAssignEmpty: 528 ms.
utilExtendEmpty: 158 ms.
============================================================================
Given that this is not only an issue for Node, but as mentioned above this is used heavily in many React projects via Babel, we should probably try to do something about it soon.
Toon you mentioned a couple of times that you have ideas how to improve performance of Object.assign. Can you share those ideas on this bug?