How to quickly clear a JavaScript Object?

后端 未结 8 1147
一个人的身影
一个人的身影 2020-11-27 12:52

With a JavaScript Array, I can reset it to an empty state with a single assignment:

array.length = 0;

This makes the Array \"appear\" empty

8条回答
  •  误落风尘
    2020-11-27 13:26

    The short answer to your question, I think, is no (you can just create a new object).

    1. In this example, I believe setting the length to 0 still leaves all of the elements for garbage collection.

    2. You could add this to Object.prototype if it's something you'd frequently use. Yes it's linear in complexity, but anything that doesn't do garbage collection later will be.

    3. This is the best solution. I know it's not related to your question - but for how long do we need to continue supporting IE6? There are many campaigns to discontinue the usage of it.

    Feel free to correct me if there's anything incorrect above.

提交回复
热议问题