1

There are libraries for high precision timers in node.js and there is the window.performance.now() for the main thread https://stackoverflow.com/a/21120901/1056563. Is there anything for web-workers (which do not have access to window or the DOM)?

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560

1 Answers1

1

There's a performance property on WorkerGlobalScope. It's not super-widely supported, but then again neither are web workers.

Specifically, .now() is (apparently) available to web workers, if that's all you need.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • From this looks like we get `chrome` but not `safari`: https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/performance – WestCoastProjects Sep 13 '20 at 22:42
  • Yes also probably some mobile browsers and almost certainly IE11. – Pointy Sep 13 '20 at 22:43
  • 1
    "neither are web workers" A feature having support from IE10+ can now be called *widely supported*. And in MDN the `?` doesn't mean it's not supported, just taht the ones maintaining the support table didn't check. Safari does support performance.now in a Worker. – Kaiido Sep 13 '20 at 23:19
  • @Kalido fair enough, the MDN support thing is as you say indeterminate. Still, if I were creating a web application I would certainly plan on some kind of fallback if the `.performance` property weren't there. – Pointy Sep 13 '20 at 23:31
  • 1
    Yeah, more importantly, and I forgot to mention that in my previous comment, performance.now lost a great deal of precision because of meltdown and spectre. – Kaiido Sep 13 '20 at 23:42