tokoph
tokoph

Reputation: 118

Do unused variables get GC'd if not used within a closure?

Assuming the following code:

const someFunction = (someString, largeObject) => {
  console.log(largeObject.huge);

  setTimeout(() => {
    console.log(someString);
  }, 10000000000000);
};

someFunction('something', { huge: 'object', tons: 'of data' });

someFunction uses the largeObject and then creates an anonymous function to be called way in the future. The new function doesn't use the largeObject at all.

Does the largeObject get garbage collected after someFunction returns or does it stick around because the anonymous function has a closure over the scope?

Upvotes: 0

Views: 107

Answers (1)

selvakumar
selvakumar

Reputation: 654

As long as you can't reference the classInstance variable anymore, it wil be GC'd. Hence largeObject will be marked for GC

Upvotes: 1

Related Questions