Underscrore 的 now 方法如何更快?

How is Underscrore's now method is faster?

我很好奇 Underscore 的 _.now 方法为何比 new Date().getTime() 更快。 我在他们的 github 代码库中看到以下内容。

// A (possibly faster) way to get the current timestamp as an integer.
_.now = Date.now || function() {
    return new Date().getTime();
};

有人可以解释一下这是怎么回事吗?

好吧,它不必构造一个 new Date 对象,利用 Date.now 提供的优势。唯一的问题是浏览器支持,所以他们包含了一个后备方案。简单地包含一个 polyfill

可能也是一个更好的主意
if (typeof Date.now != "function")
  Date.now = function() { return new Date().getTime(); };

并使用它而不是提倡自己的辅助函数。

通过使用Date.now,您无需构造对象。但IE < 9不支持。在这种情况下,它使用旧的 new Date().getTime 函数。

您可以参考此link了解浏览器支持的详细信息:http://caniuse.com/#search=Date.now