Home > Software design >  Updating Date object to now()
Updating Date object to now()

Time:06-27

I need to read the current time in one second intervals.
Should I rely on garbage collection to take care of regularly created instances of Date like mytime = new Date() or should I update mytime with the static Date.now() - and how?


Put differently:
let mytime = new Date();
...
// regularly
// A)
mytime = new Date(); // replace with current instance, rely on garbage collection
// OR
// B)
mytime.setUTCMilliseconds(Date.now()); // update original instance

CodePudding user response:

Unless for some reason you need to make sure that you have only a single shared instance and that it gets mutated (hint: you really shouldn't), just create new date objects whenever you need them. GC will take care of the old ones just fine.

  • Related