Skip to content

Commit 1c6ed0f

Browse files
author
Olivier Payen
committed
Use monotonic clock for performance.now()
1 parent e68aa6a commit 1c6ed0f

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

React/CxxBridge/RCTJSIExecutorRuntimeInstaller.mm

Lines changed: 8 additions & 2 deletions
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

Lines changed: 8 additions & 2 deletions
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)