From 1721efb54ff9cc4f577b5ae27f13fcf56801a92c Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Thu, 2 Dec 2021 12:23:20 -0800 Subject: [PATCH] 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( 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: https://github.com/facebook/react-native/pull/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 --- React/CxxBridge/RCTJSIExecutorRuntimeInstaller.mm | 5 +++-- ReactAndroid/src/main/jni/react/jni/NativeTime.cpp | 10 ++-------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/React/CxxBridge/RCTJSIExecutorRuntimeInstaller.mm b/React/CxxBridge/RCTJSIExecutorRuntimeInstaller.mm index ae811e14cb0..26b4e21ed72 100644 --- a/React/CxxBridge/RCTJSIExecutorRuntimeInstaller.mm +++ b/React/CxxBridge/RCTJSIExecutorRuntimeInstaller.mm @@ -8,6 +8,7 @@ #include "RCTJSIExecutorRuntimeInstaller.h" #import +#include namespace facebook { namespace react { @@ -21,8 +22,8 @@ JSIExecutor::RuntimeInstaller RCTJSIExecutorRuntimeInstaller(JSIExecutor::Runtim bindNativeLogger(runtime, iosLoggingBinder); PerformanceNow iosPerformanceNowBinder = []() { - // CACurrentMediaTime() returns the current absolute time, in seconds - return CACurrentMediaTime() * 1000; + auto time = std::chrono::system_clock::now().time_since_epoch(); + return std::chrono::duration_cast(time).count(); }; bindNativePerformanceNow(runtime, iosPerformanceNowBinder); diff --git a/ReactAndroid/src/main/jni/react/jni/NativeTime.cpp b/ReactAndroid/src/main/jni/react/jni/NativeTime.cpp index 05eb72b49bc..f0ed4ced09b 100644 --- a/ReactAndroid/src/main/jni/react/jni/NativeTime.cpp +++ b/ReactAndroid/src/main/jni/react/jni/NativeTime.cpp @@ -12,14 +12,8 @@ namespace facebook { namespace react { double reactAndroidNativePerformanceNowHook() { - auto time = std::chrono::steady_clock::now(); - auto duration = std::chrono::duration_cast( - time.time_since_epoch()) - .count(); - - constexpr double NANOSECONDS_IN_MILLISECOND = 1000000.0; - - return duration / NANOSECONDS_IN_MILLISECOND; + auto time = std::chrono::system_clock::now().time_since_epoch(); + return std::chrono::duration_cast(time).count(); } } // namespace react