Skip to content

Commit 114d31f

Browse files
Olivier Payenfacebook-github-bot
Olivier Payen
authored andcommitted
Use monotonic clock for performance.now() (#33983)
Summary: In #32695, the `Performance.now()` implementation changed to use unix epoch timestamps instead of a monotonic clock. This is problematic, because it means that performance measurements get skewed if the device clock changes between two measurements. With this change, the clock is now monotonic (and the implementation stays consistent between platforms). More details and repro steps can be found in [this issue](#33977) Closes #33977 ## Changelog [General] [Fixed] - Use monotonic clock for performance.now() Pull Request resolved: #33983 Test Plan: Run on iOS and Android: ``` const now = global.performance.now() console.log(`${Platform.OS}: ${now}`) ``` Reviewed By: JoshuaGross, cipolleschi Differential Revision: D37066999 Pulled By: dmitryrykun fbshipit-source-id: 298547bf39faea1b025c17ff2d2e1a03f929865b
1 parent 4f732ba commit 114d31f

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

React/CxxBridge/RCTJSIExecutorRuntimeInstaller.mm

+8-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@
2222
bindNativeLogger(runtime, iosLoggingBinder);
2323

2424
PerformanceNow iosPerformanceNowBinder = []() {
25-
auto time = std::chrono::system_clock::now().time_since_epoch();
26-
return std::chrono::duration_cast<std::chrono::milliseconds>(time).count();
25+
auto time = std::chrono::steady_clock::now();
26+
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(
27+
time.time_since_epoch())
28+
.count();
29+
30+
constexpr double NANOSECONDS_IN_MILLISECOND = 1000000.0;
31+
32+
return duration / NANOSECONDS_IN_MILLISECOND;
2733
};
2834
bindNativePerformanceNow(runtime, iosPerformanceNowBinder);
2935

ReactAndroid/src/main/jni/react/jni/NativeTime.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ namespace facebook {
1212
namespace react {
1313

1414
double reactAndroidNativePerformanceNowHook() {
15-
auto time = std::chrono::system_clock::now().time_since_epoch();
16-
return std::chrono::duration_cast<std::chrono::milliseconds>(time).count();
15+
auto time = std::chrono::steady_clock::now();
16+
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(
17+
time.time_since_epoch())
18+
.count();
19+
20+
constexpr double NANOSECONDS_IN_MILLISECOND = 1000000.0;
21+
22+
return duration / NANOSECONDS_IN_MILLISECOND;
1723
}
1824

1925
} // namespace react

0 commit comments

Comments
 (0)