javascript 删除node.js数组中对象的正确方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9849729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:01:47  来源:igfitidea点击:

the correct way to delete object in an array for node.js?

javascriptnode.jsgarbage-collection

提问by Harold Chan

I have written a function for deleting object preiodically.

我已经编写了一个用于定期删除对象的函数。

function add(variable, expireSecond){
    this.list[variable] = {"expireSecond": expireSecond, "addTime": (new Date().getTime()) / 1000};
}

function deleteExpiredObject(){
    var currentTime = (new Date().getTime()) / 1000;
    for (var key in this.list) {
        var item = this.list[key];
        if (item.expireSecond + item.addTime < currentTime){
            delete this.list[key];
        }
    }
}

When I use it, I tried to do the following:

当我使用它时,我尝试执行以下操作:

add(xxx[1], 300);

But when I called deleteExpiredObject(), it seems that the memory is not free after the object is expired. Is it due to non-zero reference of the object in xxx[1]? How to solve? Is there any library I can use?

但是当我调用时deleteExpiredObject(),似乎对象过期后内存没有空闲。是由于 中对象的非零引用xxx[1]吗?怎么解决?有我可以使用的图书馆吗?

Thanks!

谢谢!

采纳答案by Timothy Strimple

This is more of a Javascript question than pertaining directly to node.js. Delete in Javascript is used to remove properties from an object. If that property references an object, the object isn't deleted, but if there are no more references to it, then it should be cleaned up on the next garbage collection cycle.

这更像是一个 Javascript 问题,而不是直接与 node.js 相关。Javascript 中的删除用于从对象中删除属性。如果该属性引用了一个对象,则该对象不会被删除,但如果不再有对它的引用,则应在下一个垃圾回收周期中清除它。

Here are a few more questions related to Javascript and the delete keyword which you may find useful:

以下是一些与 Javascript 和 delete 关键字相关的问题,您可能会发现它们很有用:

`new` without `delete` on same variable in Javascript

在 Javascript 中的同一变量上没有 `delete` 的 `new`

When should I use delete vs setting elements to null in JavaScript?(Closed as dupe, but has good answers)

什么时候应该在 JavaScript 中使用 delete 和将元素设置为 null?(被骗了,但有很好的答案)

Deleting Objects in JavaScript

在 JavaScript 中删除对象