Why es6 recommends using assign instead of adding attributes through periods

Mar.10,2021

because browsers optimize
static language A.X like java, as long as you remember how many bits are needed in front of x, what x itself is, and how many bits it needs to occupy, so the assembly code is very short, and it runs very quickly
js a.x needs to save itself whether it is a string or an array, an object or a number, and its location in memory. This is very slow
so the V8 bosses think of a way, ah, in order to maintain speed, ES6 advocates keeping objects static, and they generate hidden classes, but if you change the type of a class attribute, the string variable array, the browser has to generate another hidden class from scratch. This is slower
, so it is recommended that languages like typescript
specifically refer to v8 Why is it so fast


ES6 does not recommend how to write it, which is recommended by the author of this article.

actually this is related to Immutable , that is, whether an object is mutable.

performance is an indirect benefit, and a more direct benefit is to make changes in variables predictable, more intuitive, and so on.

for more information, please refer to this article Immutable Data Structures and JavaScript

.

some JS engines such as V8 optimize objects in the same shape, and adding and deleting attributes will change the shape to cancel the optimization. (V8 optimized to delete the last (added) attribute)

also see you tag React, in React to do this is more to consider immutability, it is convenient for React to determine whether the object has changed or not. Libraries like Redux require this to keep state clean.


since it is recommended by others, accept it. This is essentially an optimization of the language. ES6 advocates keeping the object static, while Object.assign () can copy the object deeply, which ensures the static state of the object.

Menu