Skip to content

Commit 1721efb

Browse files
mrousavyfacebook-github-bot
authored andcommitted
fix: Use same implementation for performance.now() on iOS and Android (#32695)
Summary: I've noticed that the `performance.now()` implementations differ on iOS and Android. iOS: ```objc PerformanceNow iosPerformanceNowBinder = []() { // CACurrentMediaTime() returns the current absolute time, in seconds return CACurrentMediaTime() * 1000; }; ``` Android: ```c++ double reactAndroidNativePerformanceNowHook() { auto time = std::chrono::steady_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>( time.time_since_epoch()) .count(); constexpr double NANOSECONDS_IN_MILLISECOND = 1000000.0; return duration / NANOSECONDS_IN_MILLISECOND; } ``` For consistency, I thought why not just use the same implementation on both iOS and Android. It also seems more logical to use Chrono on iOS, since it has nanosecond precision and we just multiply it to milliseconds, whereas `CACurrentMediaTime` multiplies to seconds, and we divide it down to milliseconds again. ## Changelog (internal change only) Pull Request resolved: #32695 Test Plan: Run on iOS and Android: ```ts const now = global.performance.now() console.log(`${Platform.OS}: ${now}`) ``` Reviewed By: feedthejim Differential Revision: D32793838 Pulled By: ShikaSD fbshipit-source-id: e7967780be95956a75a3a3757311af0077976d23
1 parent 90f0de9 commit 1721efb

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed

React/CxxBridge/RCTJSIExecutorRuntimeInstaller.mm

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "RCTJSIExecutorRuntimeInstaller.h"
99

1010
#import <React/RCTLog.h>
11+
#include <chrono>
1112

1213
namespace facebook {
1314
namespace react {
@@ -21,8 +22,8 @@
2122
bindNativeLogger(runtime, iosLoggingBinder);
2223

2324
PerformanceNow iosPerformanceNowBinder = []() {
24-
// CACurrentMediaTime() returns the current absolute time, in seconds
25-
return CACurrentMediaTime() * 1000;
25+
auto time = std::chrono::system_clock::now().time_since_epoch();
26+
return std::chrono::duration_cast<std::chrono::milliseconds>(time).count();
2627
};
2728
bindNativePerformanceNow(runtime, iosPerformanceNowBinder);
2829

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

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

1414
double reactAndroidNativePerformanceNowHook() {
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;
15+
auto time = std::chrono::system_clock::now().time_since_epoch();
16+
return std::chrono::duration_cast<std::chrono::milliseconds>(time).count();
2317
}
2418

2519
} // namespace react

0 commit comments

Comments
 (0)