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

Skip to content

Commit ae8e760

Browse files
committed
Faster System.nanoTime(), both in JS and Wasm.
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.
1 parent ad1586d commit ae8e760

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

javalib/src/main/scala/java/lang/System.scala

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,17 @@ object System {
7171
js.Date.now().toLong
7272

7373
private object NanoTime {
74-
val getHighPrecisionTime: js.Function0[scala.Double] = {
75-
if (js.typeOf(global.performance) != "undefined" && !Utils.isUndefined(global.performance.now)) {
76-
() => global.performance.now().asInstanceOf[scala.Double]
77-
} else {
78-
() => js.Date.now()
79-
}
74+
val highPrecisionTimer: js.Dynamic = {
75+
if (js.typeOf(global.performance) != "undefined" && !Utils.isUndefined(global.performance.now))
76+
global.performance
77+
else
78+
global.Date
8079
}
8180
}
8281

8382
@inline
8483
def nanoTime(): scala.Long =
85-
(NanoTime.getHighPrecisionTime() * 1000000).toLong
84+
(NanoTime.highPrecisionTimer.now().asInstanceOf[scala.Double] * 1000000).toLong
8685

8786
// arraycopy ----------------------------------------------------------------
8887

0 commit comments

Comments
 (0)