Javascript - 可以重用 Date() 对象吗?

发布于 2024-12-09 17:26:44 字数 228 浏览 0 评论 0原文

是否可以创建一个新的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

太阳男子 2024-12-16 17:26:44

除非您单独存储它们,否则您的日期对象将被自动垃圾收集。此外,您可以在每次迭代时将当前日期存储到同一变量,而不必担心内存溢出。

例如:

var current = new Date()
for (var idx = 0; idx <= frameCount; ++idx) {
    current = new Date();

      // Do processing...
}

在这种情况下,您不会使用越来越多的内存,因为旧日期在被覆盖后将被垃圾收集。

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:

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.

围归者 2024-12-16 17:26:44

如果您实际上不需要日期,而是以毫秒为单位的时间,请使用 Date.now(),这样您就不必创建 Date 对象。

var t = Date.now(); // 1318479105311

您可以使用以下命令将其填充到旧版浏览器中:

if( !Date.now ) Date.now = function(){ return +(new 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 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); };
抠脚大汉 2024-12-16 17:26:44

如果您有现代浏览器,请调用

Date.now()

它返回包含纪元时间的数字。

数字是 JavaScript 中的基元,因此您不会创建任何新对象。但正如其他人所说,新物体并没有什么大不了的。

不过,您可能会对这种方法感兴趣,特别是如果您不需要年、月和日,并且对纪元时间感到满意的话。我想如果您真的想要重用日期对象,您可以使用从纪元时间派生的值来调用设置器,但我认为这种方法不会给您带来任何好处。

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.

自由如风 2024-12-16 17:26:44

现代浏览器有 Date.now() 方法,它返回 epoch 时间而不创建对象。 IE 从版本 9 开始就有它。

不过我不确定这会带来多大的优势,创建一个对象非常便宜。正如 @icktoofay 指出的,如果您不保留旧日期,它们将被垃圾收集。

编辑:这是两者的基准Date.now 实际上在 Chrome 中速度是原来的两倍,但在 > 中3mhz 区域 我认为这对于动画来说并不重要。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文