性能 - Date.now()vs Date.getTime() [英] Performance - Date.now() vs Date.getTime()

查看:224
本文介绍了性能 - Date.now()vs Date.getTime()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var timeInMs = Date.now();

MDN

vs。

var timeInMs = new Date(optional).getTime();

MDN

两者之间是否有任何区别,除了语法和在第二个版本中通过可选项设置日期(不是当前)的能力?

Is there any difference between the two, besides the syntax and the ability to set the Date (to not the current) via optional in the second version?

Date.now()更快 - 查看 jsperf

Date.now() is faster - check out the jsperf

推荐答案

这些都是相同的(编辑语义;使用 .now(),性能稍好一点):

These things are the same (edit semantically; performance is a little better with .now()):

var t1 = Date.now();
var t2 = new Date().getTime();

然而,任何已经创建的日期实例在构造时(或在其设置的任何时间/日期)被冻结。也就是说,如果你这样做:

However, the time value from any already-created Date instance is frozen at the time of its construction (or at whatever time/date it's been set to). That is, if you do this:

var now = new Date();

然后等待一段时间,随后调用 now.getTime() 将告知变量设置点的时间。

and then wait a while, a subsequent call to now.getTime() will tell the time at the point the variable was set.

这篇关于性能 - Date.now()vs Date.getTime()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆