164
var timeInMs = Date.now();

per MDN

vs.

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

per 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() is faster - check out the jsperf

3

7 Answers 7

146

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();

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

3
  • 1
    Do you think it would be more performant to create one date object in the beginnign of the program and then just update that date object (dateObj.setTime(Date.now())) or create new date objects every time you do something asynchronous that needs to access Date methods (such as dateObj.getMinutes()) ?
    – doubleOrt
    Jan 21, 2018 at 19:08
  • 7
    @Taurus modern JavaScript runtimes are extremely good at object creation and garbage collection. Unless you're working on some sort of real-time game kernel, there's no reason at all to worry about it. Write code that looks good and isn't fragile.
    – Pointy
    Jan 21, 2018 at 19:11
  • 1
    not supposed to say thanks but thanks (I hope I haven't done this more than once).
    – doubleOrt
    Jan 21, 2018 at 19:27
79

They are effectively equivalent, but you should use Date.now(). It's clearer and about twice as fast.

Edit: Source: http://jsperf.com/date-now-vs-new-date

6
  • 2
    Is this because Date(optional).getTime(); has to allocate space to get a new Date object before getting the current time?
    – Charlie G
    Sep 20, 2012 at 17:07
  • Probably, yes. I would expect that it has more to do with everything that the Date constructor is doing rather than the actual allocation of the object, though.
    – jonvuri
    Sep 20, 2012 at 17:43
  • Yeah, I added that hasily - I meant the allocation and everything that goes along with creating an object.
    – Charlie G
    Sep 20, 2012 at 17:53
  • 2
    @vegemite4me Mentioned link is broken!
    – cmcodes
    Jun 8, 2022 at 12:14
  • @cmcodes Not my answer, but you can try this: jsbench.me/f5k9ckm6lh/1 Jun 8, 2022 at 21:25
6

When you do (new Date()).getTime() you are creating a new Date object. If you do this repeatedly, it will be about 2x slower than Date.now()

The same principle should apply for Array.prototype.slice.call(arguments, 0) vs [].slice.call(arguments, 0)

3

Sometimes it's preferable to keep some time tracking variable in a Date object format rather than as just a number of milliseconds, to have access to Date's methods without re-instantiating. In that case, Date.now() still wins over new Date() or the like, though only by about 20% on my Chrome and by a tiny amount on IE.

See my JSPERF on

timeStamp2.setTime(Date.now()); // set to current;

vs.

timeStamp1 = new Date(); // set to current;

http://jsperf.com/new-date-vs-settime

2

Yes, that is correct; they are effectively equivalent when using the current time.

1

Date.now() call the static method and this is enough to be preferred, perfoming a test:


# cat test.ts

const limit = 10000000 //ten milion
let startTime = Date.now()
let val = 0

for (let i=0; i<=limit; i++) {
    const val1 = Date.now()
}

let diff = Date.now() - startTime

console.log('Time Date.now(): ' + diff + ' ms')

startTime = Date.now()
val=0
for (let i=0; i<=limit; i++) {
    const val1 = new Date().getTime()
}

diff = Date.now() - startTime

console.log('Time with new Date().getTime: ' + diff + ' ms' )

Test execution:


# node test.ts
Time Date.now(): 818 ms
Time with new Date().getTime: 1625 ms

Date.now() is 50% faster

-2

Date.now() is calling the static method now() of the class Date. While new Date().getTime() can be divided into two steps:

  1. new Date(): Call the constructor() method of Date class to initialize an instance of Date class.
  2. Call getTime() method of the instance we just initialize.

MDN web docs classifies Date.now() into static method of Date, and Date.prototype.getTime() into instance method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.