Memory Management in JS
Charlie Barajas

Charlie Barajas @char1iedevos

About: Being a backend developer in your closet, writing all the scripts...

Joined:
May 31, 2025

Memory Management in JS

Publish Date: Jun 18
4 1

Hello,

Today, I took a lecture from freecodecamp.com's Full Stack JavaScript course and learned about memory management. When a variable is created, it retains its memory address.

function createLargeArray() {
let largeArray = new Array(1000000);
return function() {
console.log(largeArray.length);
};
}

let printArrayLength = createLargeArray();
printArrayLength();

This snippet demonstrates that although createLargeArray() can't be trashed, it is still closed, and the return access means that more memory is used than expected.

Comments 1 total

  • Karan
    KaranJun 19, 2025

    A more memory-efficient approach would be to extract only what you need

    function createLargeArray() {
    let largeArray = new Array(1000000);
    let arrayLength = largeArray.length;
    largeArray = null;
    return function() {
    console.log(arrayLength);
    };
    }

Add comment