Skip to content

Commit

Permalink
fix: Use same implementation for performance.now() on iOS and Andro…
Browse files Browse the repository at this point in the history
…id (#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
  • Loading branch information
mrousavy authored and facebook-github-bot committed Dec 2, 2021
1 parent 90f0de9 commit 1721efb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
5 changes: 3 additions & 2 deletions React/CxxBridge/RCTJSIExecutorRuntimeInstaller.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "RCTJSIExecutorRuntimeInstaller.h"

#import <React/RCTLog.h>
#include <chrono>

namespace facebook {
namespace react {
Expand All @@ -21,8 +22,8 @@
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<std::chrono::milliseconds>(time).count();
};
bindNativePerformanceNow(runtime, iosPerformanceNowBinder);

Expand Down
10 changes: 2 additions & 8 deletions ReactAndroid/src/main/jni/react/jni/NativeTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,8 @@ namespace facebook {
namespace react {

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;
auto time = std::chrono::system_clock::now().time_since_epoch();
return std::chrono::duration_cast<std::chrono::milliseconds>(time).count();
}

} // namespace react
Expand Down

0 comments on commit 1721efb

Please sign in to comment.