Skip to content

Closure in Javascript: What, How to use, memory leaks

Closure in Javascript: What, How to use, memory leaks

Closure in Javascript refers to the ability of a function to access variables that are declared outside of its own scope. This means that a function can “remember” the values of its surrounding variables even after those variables are no longer in scope.

Closure in Javascript

Closures are created whenever a function is defined inside another function. The inner function can access the outer function’s variables and parameters, even after the outer function has returned. This allows for a lot of flexibility in how functions can be used.

Example of a closure in JavaScript:

function outerFunction() {
  const outerVariable = "I am outside!";

  function innerFunction() {
    console.log(outerVariable);
  }

  return innerFunction;
}

const innerFunc = outerFunction();
innerFunc(); // output: "I am outside!"

In this example, outerFunction returns the innerFunction. When innerFunction is called, it logs the value of outerVariable to the console, even though outerVariable is no longer in scope.

This is possible because innerFunction has a reference to the scope of outerFunction in which it was defined. When outerFunction is called, it creates a new execution context with its own variable environment. The innerFunction has a reference to that variable environment, which is why it can still access outerVariable.

Memory Leak

A closure leak occurs when a function that is no longer needed continues to hold onto references to variables or objects, preventing them from being garbage collected. This can lead to memory leaks and reduced performance in JavaScript applications.

Closure leaks occur when inner functions that were defined inside an outer function are still being referenced from outside that outer function. Because of the way closures work in JavaScript, the inner functions have access to the variables and objects in the outer function’s scope, and if any of those variables or objects hold references to other objects, they will remain in memory as long as the inner functions continue to exist.

Example of a closure leak:

function outerFunction() {
  const someObject = {
    prop1: "value1",
    prop2: "value2"
  };

  function innerFunction() {
    console.log(someObject.prop1);
  }

  return innerFunction;
}

const innerFunc = outerFunction();

In this example, innerFunction has access to the someObject variable in the outer function’s scope. If innerFunc is never called again, but someObject continues to exist, then someObject will not be garbage collected because it is still being referenced by the closure created by innerFunction.

To avoid closure leaks, it’s important to make sure that any inner functions that are no longer needed are removed from memory. This can be done by either setting the reference to the inner function to null, or by removing the reference to the outer function altogether.

Conclusion

Closures are commonly used in JavaScript for things like event handlers, callbacks, and maintaining private variables. Understanding closures is an important concept for any JavaScript developer. In general, it’s good practice to avoid creating closures that hold onto large or unnecessary objects, and to make sure that closures are properly cleaned up when they are no longer needed.

Further Readings

Find index in Javascript: How to use JS inbuilt methods

Please share