Skip to content

Commit aef72c0

Browse files
authored
feat: Add performance.now() to Worklets (#2679)
## 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
1 parent 6c56b7d commit aef72c0

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Common/cpp/Tools/RuntimeDecorator.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "RuntimeDecorator.h"
2+
#include <chrono>
23
#include <memory>
34
#include <unordered_map>
45
#include "LayoutAnimationsProxy.h"
@@ -83,6 +84,22 @@ void RuntimeDecorator::decorateRuntime(
8384
jsi::PropNameID::forAscii(rt, "_setGlobalConsole"),
8485
1,
8586
setGlobalConsole));
87+
88+
rt.global().setProperty(
89+
rt,
90+
"_chronoNow",
91+
jsi::Function::createFromHostFunction(
92+
rt,
93+
jsi::PropNameID::forAscii(rt, "_chronoNow"),
94+
0,
95+
[](jsi::Runtime &rt,
96+
const jsi::Value &thisValue,
97+
const jsi::Value *args,
98+
size_t count) -> jsi::Value {
99+
double now = std::chrono::system_clock::now().time_since_epoch() /
100+
std::chrono::milliseconds(1);
101+
return jsi::Value(now);
102+
}));
86103
}
87104

88105
void RuntimeDecorator::decorateUIRuntime(

src/reanimated2/core.ts

+3
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@ if (!NativeReanimatedModule.useOnlyV1) {
383383
info: runOnJS(capturableConsole.info),
384384
};
385385
_setGlobalConsole(console);
386+
global.performance = {
387+
now: global._chronoNow,
388+
};
386389
})();
387390
}
388391

0 commit comments

Comments
 (0)