Skip to content

Use Performance API for high-precision time on JS#2895

Merged
armanbilge merged 15 commits into
typelevel:series/3.xfrom
armanbilge:feature/js-precise-time
Apr 7, 2022
Merged

Use Performance API for high-precision time on JS#2895
armanbilge merged 15 commits into
typelevel:series/3.xfrom
armanbilge:feature/js-precise-time

Conversation

@armanbilge

Copy link
Copy Markdown
Member

Follow-up to #2416.

Always an adventure on JS :) there's nothing in the ES standard, but amazingly both browsers and Node.js have implemented the same APIs for once! With of course the caveat that for whatever reason performance.timeOrigin is "experimental technology" on the web 🙃

djspiewak
djspiewak previously approved these changes Mar 20, 2022
@armanbilge

Copy link
Copy Markdown
Member Author

One learning from this PR is that IO.cede on browsers very nearly takes a millisecond ...

@armanbilge

Copy link
Copy Markdown
Member Author

Urgh, extremely annoying! On browsers IO.cede is too slow; on Node.js, without it is too fast.

@armanbilge

armanbilge commented Mar 20, 2022

Copy link
Copy Markdown
Member Author

Hmm, even with the cede Node.js is having issues :( I can't replicate locally though.

[error]   x use high-precision time
[error]    0 microseconds is equal to 0 nanoseconds (/home/runner/work/cats-effect/cats-effect/tests/js/target/scala-2.13/cats-effect-tests-test-fastopt/https:

Update: the problem is with Node 14. Hmmm.

@armanbilge

Copy link
Copy Markdown
Member Author

Node 14 is LTS for one more year. I guess we should keep it in CI :(
https://nodejs.org/en/about/releases/

private[this] val nowMicrosImpl: () => Long = {
def test(performance: Performance): Try[Performance] = Try {
// take it for a spin
require(!(performance.timeOrigin + performance.now()).isNaN)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exceptions are pretty expensive. Can we turn this into a simple if/else?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even for a one-time initialization?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you're probably right :( I've used Try in many places, maybe I should dial those back.

@armanbilge

Copy link
Copy Markdown
Member Author

This is looking to be flaky on Chrome. Unfortunately the resolution there is 100 microseconds (versus 5 microseconds on Firefox with privacy.reduceTimerPrecision=false). I guess I can try cede again, but that may be dangerously slow 😅

@armanbilge armanbilge added this to the v3.4.0 milestone Mar 25, 2022
djspiewak
djspiewak previously approved these changes Apr 7, 2022
@djspiewak

Copy link
Copy Markdown
Member

@armanbilge This is ready to go once the conflicts are resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants