11

I'm new to Nodejs and I'm confused about something when timing:

If Nodejs is Javascript, then why Javascript's performance.now() doesn't work on Nodejs and I'm forced to use console.time() or the like?

How can I know what other Javascript functions doesn't work on Node?

Thanks!

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Fer B.
  • 425
  • 5
  • 11
  • 3
    The `performance` API is a web browser thing; it's not part of JavaScript proper, just like the DOM is not part of JavaScript. – Pointy Jun 18 '16 at 16:07
  • In Node, you probably want `process.hrtime()`. Anyhow, some API's are host-specific. Best way to know for sure is test. – Alexander O'Mara Jun 18 '16 at 16:09

1 Answers1

11

Update: Although this was correct at the time it was written, this answer is now obsolete and modern versions of Node do support performance.now()

First, we have to be very clear about what JavaScript does -- and does not include.

The definition for what JavaScript includes is the EMCA-262 guide. This is the only thing that JavaScript is required to have.

Everything else - from the DOM to the performance object are additions provided by the host environment.

Some -- many -- browsers have chosen to add performance.now() but that is NOT part of JavaScript -- just something that JavaScript can call.

nodeJS currently doesn't support it -- at least not out-of-the-box, but it looks like someone created a module that does give you that ability.

I've not tried it, just did a quick google for 'node performance.now` and this was the first hit: https://www.npmjs.com/package/performance-now

Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
  • 2
    Node v8.5.0 has added [Performance Timing API](https://nodejs.org/api/perf_hooks.html#perf_hooks_performance_timing_api), which includes the [`performance#now()`](https://nodejs.org/api/perf_hooks.html#perf_hooks_performance_now). – Gajus Sep 30 '17 at 12:44
  • 2
    `import { performance } from 'perf_hooks'` Worked great in 11 – Jackie Jul 31 '18 at 02:01