How is the Underscrore method faster now?

I am wondering how the Underscore _.now method is faster than just new Date().getTime() . I see the following on my github code base.

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

Can someone explain what is going on here?

+5
source share
2 answers

Well, he does not need to create a new Date object, taking advantage of the advantage provided by Date.now . The only problem with this was browser support, so they included a backup. It might be a better idea to just turn on polyfill

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

and use this instead of defending your own helper function.

+3
source

Using Date.now, you do not need to create an object. But it is not supported in IE <9. In this case, the old Date () function is used. Gettime

Comparison

You can refer to this link for detailed browser support: http://caniuse.com/#search=Date.now

0
source

Source: https://habr.com/ru/post/1271586/


All Articles