-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Perf: replace spread with Object.assign #1900
Conversation
|
有参考资料说明 Object.assign 比 spread operator 更快吗?我查到的资料是 spread operator 更快 |
@SignDawn Yes, if you look at that article, it IS correct. const obj = { foo: 1, bar: 2 };
const resultSpread = { baz: 3, ...obj };
const resultAssign = Object.assign({}, { baz: 3}, obj ) In this case performance will be almost identical, like in the article you mentioned. On my machine with Node.js v18 it's:
But once you do this (the way spread operator is used in ahooks): const obj = { foo: 1, bar: 2 };
const obj2 = { baz: 3 };
const resultSpread = { ...obj, ...obj2 };
const resultAssign = Object.assign({}, obj, obj2 ) Performance of Spread drops significantly:
If you add yet another object: const obj = { foo: 1, bar: 2 };
const obj2 = { baz: 3 };
const obj3 = { another: 4 }
const resultSpread = { ...obj, ...obj2, ...obj3 };
const resultAssign = Object.assign({}, obj, obj2, obj3 ) You get even worse results:
You can test it yourself with "benchmark" package on Node.js. Summary: I haven't found a single csse when Object.spread is actually faster that Object.assign. |
简单说,'Object spread' 使用的越多,性能越差 |
In Fact, there is no need to change... Babel will transfrom when build // source
{ ...prevState, ...newState }
// es
var __assign = this && this.__assign || function () {
__assign = Object.assign || function (t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
}
return t;
};
return __assign.apply(this, arguments);
};
__assign(__assign({}, prevState), newState) But if you use // source
Object.assign({}, prevState, newState)
// without @babel/plugin-transform-object-assign
Object.assign({}, prevState, newState)
// with @babel/plugin-transform-object-assign
function _extends() { ... }
_extends({}, prevState, newState) So I think this pr should be revert, but thanks for your contribution! |
* perf: replace spread with Object.assign * chore: update version * chore: update pnpm-lock Co-authored-by: lxr <[email protected]> Co-authored-by: 潇见 <[email protected]>
* perf: replace spread with Object.assign * chore: update version * chore: update pnpm-lock Co-authored-by: lxr <[email protected]> Co-authored-by: 潇见 <[email protected]>
[中文版模板 / Chinese template]
🤔 This is a ...
🔗 Related issue link
💡 Background and solution
Object.assign is at least 2x times faster than spread operator. This can be noticable when one needs to update state object quite often (for example - on every user keystroke). In this case performance is important.
📝 Changelog
☑️ Self Check before Merge