George Paouris
George Paouris

Reputation: 635

What happens to variables when are not used in a module?

utils.js

const array = [1,2,3,4,5,6,7,8];

export default (props)=>{return <div>{array.map(()=>do something)}</div>}

index.js

import Comp from './utils.js';

I know that when you require a module, it remains in memory cache, so if you call it twice it, the module will not run and give you back the exported functions.

In this case we have a const variable. The exported function has a reference to variable and because the exported function remains in cache that means that the const variable remains also and is not GB. Is this right? If not what does actually happen?

Upvotes: 1

Views: 189

Answers (1)

Stefan Steiger
Stefan Steiger

Reputation: 82186

Yes - the const variable stays, because you never set it to NULL.

This is because the closure-variables are still used when you keep a reference to them in the exported default that is cached. So the GC can never collect this variable/const (assuming the GC is not buggy).

See also what is this "require".

Upvotes: 1

Related Questions