search

Home  >  Q&A  >  body text

Are the efficiency of js replacement and adding object members the same?

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?

曾经蜡笔没有小新曾经蜡笔没有小新2770 days ago733

reply all(3)I'll reply

  • 为情所困

    为情所困2017-06-12 09:30:17

    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.

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-06-12 09:30:17

    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".

    reply
    0
  • 阿神

    阿神2017-06-12 09:30:17

    I agree with @biancheng’s point of view

    reply
    0
  • Cancelreply