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

20,754

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.

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

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

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

Deleting Objects in JavaScript

Share:
20,754
Harold Chan
Author by

Harold Chan

Updated on July 09, 2022

Comments

  • Harold Chan
    Harold Chan almost 2 years

    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?

    Thanks!