Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

sjrd
Copy link
Member

@sjrd sjrd commented Aug 28, 2025

No description provided.

@sjrd sjrd requested a review from gzm0 August 28, 2025 12:49
Copy link
Contributor

@gzm0 gzm0 left a comment

Choose a reason for hiding this comment

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

Nit about the last commit title: IMHO the title explains the why, not the what (but it should explain the what).

*/
val start = System.nanoTime()
val elapsed = System.nanoTime() - start
assertTrue(elapsed.toString(), elapsed >= 0L && elapsed < 100000000L)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure we should assume monotonicity here. In general, it is not a valid assumption to make for clocks. The JDK doc doesn't give an explicit guarantee about it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hum, nanoTime is definitely supposed to be monotonic. However I did find this bug report, which says that sometimes it incorrectly goes back in time. 😕

Copy link

Choose a reason for hiding this comment

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

We encountered this bug on a Linux box with Jvm, which then cause the Akka stream of keepAlive operator failed.

sjrd added 4 commits September 1, 2025 10:08
All browsers have been supporting the official `performance.now()`
for more than 10 years, at this point.
`js.Date.now()` was already present in ECMAScript 5.1. We were
using it in the constructor of `ju.Date`, but not in the two
methods of `System`.

In this commit, we switch to `js.Date.now()` in `System`. We also
change the constructor of `ju.Date` to delegate to
`currentTimeMillis`, in order to have a single source of truth.
Instead of storing a lambda returning the high precision time,
we store the global object whose `now()` method returns the high
precision time. This is possible because the only two alternatives,
`performance.now()` and `Date.now()`, happen to share their method
name and the scale of their results.

This implementation removes a JS function call dispatch, which
makes the call faster both in JS and in Wasm.
@sjrd sjrd force-pushed the better-system-time-impl branch from ae8e760 to a06bb44 Compare September 1, 2025 08:10
@sjrd sjrd enabled auto-merge September 1, 2025 08:12
@sjrd sjrd merged commit b024d1b into scala-js:main Sep 1, 2025
3 checks passed
@sjrd sjrd deleted the better-system-time-impl branch September 6, 2025 17:36
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.

3 participants