Javascript - 可以重用 Date() 对象吗?
是否可以创建一个新的 Date() 对象来获取当前日期,然后在一段时间后,重用相同的 Date() 对象来获取新的当前时间?
看来每次您需要当前日期/时间时都必须创建一个新的 Date 对象。
在我的特定应用程序中,我想要运行动画,并且对于动画的每一帧,我需要获取当前时间。因此,每帧(可能是 1000 帧?)创建一个新的 Date 对象只会随着时间的推移增加内存使用量。
这件事有什么线索吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
除非您单独存储它们,否则您的日期对象将被自动垃圾收集。此外,您可以在每次迭代时将当前日期存储到同一变量,而不必担心内存溢出。
例如:
在这种情况下,您不会使用越来越多的内存,因为旧日期在被覆盖后将被垃圾收集。
Unless 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:
You do not use more and more memory in this case because the old date will be garbage-collected after it has been overwritten.
如果您实际上不需要日期,而是以毫秒为单位的时间,请使用
Date.now()
,这样您就不必创建Date
对象。您可以使用以下命令将其填充到旧版浏览器中:
If you don't actually want the date, but rather the time in milliseconds, use
Date.now()
so you don't have to create aDate
object.You can shim it into older browsers with:
如果您有现代浏览器,请调用
它返回包含纪元时间的数字。
数字是 JavaScript 中的基元,因此您不会创建任何新对象。但正如其他人所说,新物体并没有什么大不了的。
不过,您可能会对这种方法感兴趣,特别是如果您不需要年、月和日,并且对纪元时间感到满意的话。我想如果您真的想要重用日期对象,您可以使用从纪元时间派生的值来调用设置器,但我认为这种方法不会给您带来任何好处。
If you have a modern browser, call
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.
现代浏览器有
Date.now()
方法,它返回epoch时间而不创建对象。 IE 从版本 9 开始就有它。不过我不确定这会带来多大的优势,创建一个对象非常便宜。正如 @icktoofay 指出的,如果您不保留旧日期,它们将被垃圾收集。
编辑:这是两者的基准。
Date.now
实际上在 Chrome 中速度是原来的两倍,但在 > 中3mhz 区域 我认为这对于动画来说并不重要。Modern browsers have the
Date.now()
method which returns theepochtime 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.