-3

When a user opens a modal I create a class:

app.product = new app.Product();

What would happen if the user opens the modal over and over again throughout the use of the site?

Would I get 1000s of repeated classes, or would the original one be replaced time and time again.

Would doing this have any effect on memory/performance?

panthro
  • 22,779
  • 66
  • 183
  • 324
  • 1
    You would get a new one each time, but if you didn’t store the old one anywhere, it would be released eventually. – Ry- Aug 17 '15 at 14:31
  • 2
    Can't you test your code and see what happens? – j08691 Aug 17 '15 at 14:31
  • Is the dialog being closed before being opened again? If that causes your variable to go out of scope, it'll be garbage collected, and there will only ever be one instance at a time. – deceze Aug 17 '15 at 14:31
  • app.product will get a new instance of app.Product() every time the user hovers...this will kill any previously created one. But I think you might want to reconsider the approach. – Bas Slagter Aug 17 '15 at 14:31
  • What would be a better approach? – panthro Aug 17 '15 at 14:31
  • @panthro that depends on what you want to achieve of course. – Bas Slagter Aug 17 '15 at 14:32
  • This is a completely fine approach as far as we know unless you benchmark it and find otherwise. – Ry- Aug 17 '15 at 14:32
  • We can't know from just that one line of code. If your code retains a reference to unused objects and basically has a memory leak, then that will have an effect on memory/performance. – David Aug 17 '15 at 14:32

1 Answers1

0

Assuming that app.product is the same 'app' object and reference each time, the garbage collector should remove the memory allocated by

new app.Product();

once the object created by it is dereferenced (meaning the app.product reference now points elsewhere ie. a new object).

Here is a previous post detailing the specifics of JavaScript garbage collection.

What is JavaScript garbage collection?

Community
  • 1
  • 1
Chad Pendergast
  • 366
  • 1
  • 4
  • 11