Skip to content

Commit

Permalink
feat: Add performance.now() to Worklets (#2679)
Browse files Browse the repository at this point in the history
## Description

Adds support for measuring performance/elapsed time in millisecond precision using Chrono with `performance.now()` in Worklets.

## Changes

- Inject `_chronoNow` func in global runtime object

<!--

## Screenshots / GIFs

Here you can add screenshots / GIFs documenting your change.

You can add before / after section if you're changing some behavior.

### Before

### After

-->

## Test code and steps to reproduce

```ts
runOnUI(() => {
  'worklet'
  const start = performance.now()
  for (let i = 0; i < 1000000; i++) {}
  const end = performance.now()
  console.log(`Loop took ${end - start} ms!`)
})()
```

## Checklist

- [ ] Included code example that can be used to test this change
- [ ] Updated TS types
- [ ] Added TS types tests
- [ ] Added unit / integration tests
- [ ] Updated documentation
- [ ] Ensured that CI passes

## Related Issues

* facebook/react-native#32695
  • Loading branch information
mrousavy committed Dec 6, 2021
1 parent 6c56b7d commit aef72c0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Common/cpp/Tools/RuntimeDecorator.cpp
@@ -1,4 +1,5 @@
#include "RuntimeDecorator.h"
#include <chrono>
#include <memory>
#include <unordered_map>
#include "LayoutAnimationsProxy.h"
Expand Down Expand Up @@ -83,6 +84,22 @@ void RuntimeDecorator::decorateRuntime(
jsi::PropNameID::forAscii(rt, "_setGlobalConsole"),
1,
setGlobalConsole));

rt.global().setProperty(
rt,
"_chronoNow",
jsi::Function::createFromHostFunction(
rt,
jsi::PropNameID::forAscii(rt, "_chronoNow"),
0,
[](jsi::Runtime &rt,
const jsi::Value &thisValue,
const jsi::Value *args,
size_t count) -> jsi::Value {
double now = std::chrono::system_clock::now().time_since_epoch() /
std::chrono::milliseconds(1);
return jsi::Value(now);
}));
}

void RuntimeDecorator::decorateUIRuntime(
Expand Down
3 changes: 3 additions & 0 deletions src/reanimated2/core.ts
Expand Up @@ -383,6 +383,9 @@ if (!NativeReanimatedModule.useOnlyV1) {
info: runOnJS(capturableConsole.info),
};
_setGlobalConsole(console);
global.performance = {
now: global._chronoNow,
};
})();
}

Expand Down

3 comments on commit aef72c0

@AhmedSarhan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has this fix been added to the npm package yet?

@feniljariwala82
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still throws an error.
React native version: 0.64.3
Expo: 44.0.2
react-native-reanimated: 2.3.1

@AhmedSarhan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it still does and I don't know what's the solution I have downgraded and upgraded nothing yet

Please sign in to comment.