Are the efficiency of js replacement and adding object members the same?
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-06-12 09:28:17
0
3
772

Just like the title, there are two examples (you can imagine other examples by yourself):

// 替换的例子
let a = {'a': 123, 'b': 234, 'c': 345}
let tmp = {'a': 'qwe'}
a = {...a, tmp}
// 删除再添加的例子
let a = {'a': 123, 'b': 234, 'c': 345}
let tmp = {'a': 'qwe'}
delete a.a
a = {...a, tmp}

Who is more efficient?

曾经蜡笔没有小新
曾经蜡笔没有小新

reply all(3)
为情所困

Delete is not to mention slow, the key is that it does not directly release memory (MDN document mentioned this at the beginning). So replace it directly in your situation without thinking too much.

So what is the use of delete?

Currently, all I can think of is that it is available under inheritance. For example, there is attribute a on the prototype chain of an object, and it also defines attribute a. Then for some reason, it no longer needs its own attribute a, and only needs to use the attribute a on the prototype chain... Then delete this attribute. Lose. But even with this application, it is difficult to encounter actual application scenarios.

仅有的幸福

It’s definitely the first kind of speed.

delete a.a is a performance killer.

I mentioned it in the lecture "Front-end programmers should know some V8 knowledge".

阿神

I agree with @biancheng’s point of view

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template