简体   繁体   中英

Optimize Javascript function for memory leaks

Due to some reasons i am optimizing javascript functions and at this time i am investigating which i can optimize following code or not.

I have following array:

var usageArray = [5,10,20,10,15,18,19,20,21,24,23,28,29,27,10,10];   

I want to push value to end of this array without changing on count of nodes and create array with following structure:

[[0, 5], [1, 10], [2, 20], [3, 10], [4, 15], [5, 18], [6, 19], [7, 20], [8, 21], [9, 24], [10, 23], [11, 29], [12, 27], [13, 10], [14, 10]]

I will pass new value each 10 second with following function:

function tUsage(value) {
    usageArray = usageArray.slice(1);
    usageArray.push(value);
    var i = [];
    for (var s = 0; s < usageArray.length; ++s) {
        i.push([s, usageArray [s]]);
    }
    console.log([i]);
    //Free Memory
    i = null;
    s = null; 
} 

Is there any way which i write this function in the better way, because fire this function each 10 seconds and want to remove its memory leaks.

As per the discussion above, your entire function can be reduced to a single Array.prototype.map call:

 var usageArray = [5,10,20,10,15,18,19,20,21,24,23,28,29,27,10,10]; var newArray = usageArray.map(function (value, index) { return [index, value]; }); console.log(newArray); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM