Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Underscrore's now method is faster?

I am curious how Underscore's _.now method is faster than just new Date().getTime(). I see the following on their github codebase.

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

Can someone please explain what's going on here ?

like image 343
sbr Avatar asked Oct 18 '22 06:10

sbr


1 Answers

Well it doesn't have to construct a new Date object, using the advantage provided by Date.now. The only problem with that was that browser support, so they included a fallback. It might as well have been a better idea to simply include a polyfill

if (typeof Date.now != "function")
  Date.now = function() { return new Date().getTime(); };

and use that instead of advocating their own helper function.

like image 186
Bergi Avatar answered Oct 21 '22 04:10

Bergi