The fastest and easiest way to get the current time in milliseconds using the JS Date object

There are different ways to get the current time in milliseconds with a Date object:

 (new Date()).getTime(); +new Date(); Date.now(); 

Assuming you don't need to create an object and just need the current time in milliseconds, which would be the most efficient? In terms of performance.

EDIT: I understand that most developers don't care about this, but it can make a difference when you work in a low-tech embedded environment or just kill curiosity.

+7
performance javascript date
source share
1 answer
Wins

Date.now() . See jsperf.com test

But, as noted in the comments above, the cost of the processor is probably uninteresting compared to anything else that you will do.

@techfoobar mentions the cost of allocating Date objects (or, indeed, the cost of garbage collecting these Date objects). This may or may not be a significant gain, as Date.now () probably highlights Number objects, which will be about as expensive.

+10
source share

All Articles