Is it possible to create a new Date() object to get the current date, and then later, after a certain period of time, reuse the same Date() object to get the new current time?
It appears that you have to create a new Date object everytime you want the current date/time.
In my particular application I'm wanting to run an animation and for every single frame of the animation I need to aquire the current time. So creating a new Date object every single frame (potentially for 1000's of frames?) is just going to boost memory usage over time.
Any clues on this one?
Is it possible to create a new Date() object to get the current date, and then later, after a certain period of time, reuse the same Date() object to get the new current time?
It appears that you have to create a new Date object everytime you want the current date/time.
In my particular application I'm wanting to run an animation and for every single frame of the animation I need to aquire the current time. So creating a new Date object every single frame (potentially for 1000's of frames?) is just going to boost memory usage over time.
Any clues on this one?
Share Improve this question asked Oct 13, 2011 at 4:03 Jake WilsonJake Wilson 91.2k97 gold badges260 silver badges371 bronze badges 2-
2
If you're creating a new
Date
object but then dropping the reference to the old one then the garbage collector will get it and the memory usage won't grow over time. – icktoofay Commented Oct 13, 2011 at 4:07 -
Your intuition is right, and the answers below have some good options. It's worth noting, though, that it's more conventional to use
setTimeout()
orsetInterval()
for animation in JavaScript, and if you're targeting only modern browsers you should look intorequestAnimationFrame
. Good luck! – Jordan Running Commented Oct 13, 2011 at 4:20
4 Answers
Reset to default 5Unless you're storing them separately, your date objects are garbage collected automatically. Moreover you can create store the current date to the same variable every iteration and not have to worry about memory blooming.
For example:
var current = new Date()
for (var idx = 0; idx <= frameCount; ++idx) {
current = new Date();
// Do processing...
}
You do not use more and more memory in this case because the old date will be garbage-collected after it has been overwritten.
If you don't actually want the date, but rather the time in milliseconds, use Date.now()
so you don't have to create a Date
object.
var t = Date.now(); // 1318479105311
You can shim it into older browsers with:
if( !Date.now ) Date.now = function(){ return +(new Date); };
If you have a modern browser, call
Date.now()
which returns a number containing the epoch time.
Numbers are primitives in JavaScript so you are not creating any new objects. But as others have said, the new objects are not too big of a deal.
Still you might be interested in this approach, especially if you do not need the year, month, and day, and will be satisfied with the epoch time. I suppose if you really wanted to reuse a date object, you can call setters with values derived from the epoch time, but I don't think this approach buys you anything.
Modern browsers have the Date.now()
method which returns the epoch time without creating an object. IE has it since version 9.
I'm not sure this would give much of an advantage though, creating an object is pretty cheap. As @icktoofay pointed out, if you don't keep the old dates around they will be garbage collected.
edit: here's a benchmark on both. Date.now
is actually twice as fast in Chrome, but being in the > 3mhz zone I don't think this will matter for animations.