3

The new performance.now() function is supposed to have better accuracy than Date.now(). All of the sites I have read seem to only use the new function for measuring execution times with the aim of finding and improving performance - because Date.now() seems hopelessly inadequate for this.

But, besides not yet being fully implemented in all browsers, is there any reason to ever use Date.now() again? Or seen another way, why on earth don't they just fix Date.now()?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
DavidWalley
  • 197
  • 11
  • 3
    You would use `Date.now()` for a date. I don't think `Date.now()` was ever meant to be used for benchmarking, and now that we have other tools we don't need to. Besides, we don't want to add more overhead to `Date` when most use cases don't need the high precision. –  Aug 22 '14 at 04:09
  • 1
    "is there any reason to ever use Date.now() again?" Yes, someone wanting the current [*time value*](http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.1). Also, *performance* is a [*W3C recommendation*](http://www.w3.org/TR/hr-time/#dom-performance-now), so not part of ECMAScript and therefore may not be in non–browser host environments (where it may not have any meaning). – RobG Aug 22 '14 at 04:37
  • I had the unfortunate experience of discovering that `performance.now()` was not implemented in Safari on iPhone, even though `performance` itself was there. For the time being, it is best to sniff for the presence of `performance.now()` and fall back to `Date.now` otherwise. –  Aug 22 '14 at 04:37
  • No problem. If I just use Date.now() for a date, then I am probably not using it that often, so I wouldn't really care about a few execution cycles of overhead. The only case where I would care would be if I am calling it a lot, in which case Date.now() probably isn't accurate enough. I am struggling to think of a down-side of doing a global search and replace in my entire project. I don't really want to examine hundreds of instances of Date.now() and ask myself how much accuracy I want at that point - it seems easier to just get as much accuracy as is available. – DavidWalley Aug 22 '14 at 04:37
  • I found a site which suggested the sniff and fall-back strategy. That would eliminate the search and replace work. I am leaning to that at the moment. – DavidWalley Aug 22 '14 at 04:41

0 Answers0