Skip to content

Commit 8fdfdd3

Browse files
committed
Add benchmark for threads
1 parent 17cca5b commit 8fdfdd3

File tree

3 files changed

+98
-2
lines changed

3 files changed

+98
-2
lines changed

example/globals.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,10 @@ declare var _LABEL: string | undefined;
1212
* Sets the global console object
1313
*/
1414
declare var _setGlobalConsole: (console: any) => void;
15+
16+
declare var performance: { now: () => number };
17+
18+
/**
19+
* Logs to the native console window (Xcode logs/Android logcat)
20+
*/
21+
declare var _log: (message: string) => void | undefined;

example/src/App.tsx

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import {
77
TextInput,
88
ActivityIndicator,
99
Alert,
10+
Button,
1011
} from 'react-native';
1112
import { spawnThread } from 'react-native-multithreading';
12-
import 'react-native-reanimated';
13+
import { runOnJS, runOnUI } from 'react-native-reanimated';
1314

1415
const fibonacci = (num: number): number => {
1516
'worklet';
@@ -26,6 +27,7 @@ const fibonacci = (num: number): number => {
2627
};
2728

2829
export default function App() {
30+
const [isBenchmarking, setIsBenchmarking] = React.useState(false);
2931
const [isRunning, setIsRunning] = React.useState(false);
3032
const [input, setInput] = React.useState('5');
3133
const [result, setResult] = React.useState<number | undefined>();
@@ -53,6 +55,73 @@ export default function App() {
5355
}
5456
}, []);
5557

58+
const runBenchmark = React.useCallback(
59+
async (thread: 'react' | 'custom' | 'ui') => {
60+
const benchmark = () => {
61+
'worklet';
62+
const start = performance.now();
63+
function p(n: number) {
64+
for (var i = 2; i * i <= n; i++) {
65+
if (n % i === 0) {
66+
return false;
67+
}
68+
}
69+
return true;
70+
}
71+
72+
var sum = 0;
73+
for (var k = 2; k < 1000000; k++) {
74+
if (p(k)) {
75+
sum++;
76+
}
77+
}
78+
const end = performance.now();
79+
return {
80+
result: sum,
81+
duration: end - start,
82+
};
83+
};
84+
setIsBenchmarking(true);
85+
switch (thread) {
86+
case 'react': {
87+
for (let i = 0; i < 5; i++) {
88+
const r = benchmark();
89+
console.log(`REACT: Run #${i}: ${r.result} (took ${r.duration}ms)`);
90+
}
91+
setIsBenchmarking(false);
92+
break;
93+
}
94+
case 'custom': {
95+
await spawnThread(() => {
96+
'worklet';
97+
for (let i = 0; i < 5; i++) {
98+
const r = benchmark();
99+
// can't use console.log because that just dispatches to React-JS thread, which might be blocked.
100+
console.log(
101+
`CUSTOM: Run #${i}: ${r.result} (took ${r.duration}ms)`
102+
);
103+
}
104+
});
105+
setIsBenchmarking(false);
106+
break;
107+
}
108+
case 'ui': {
109+
runOnUI(() => {
110+
'worklet';
111+
for (let i = 0; i < 5; i++) {
112+
const r = benchmark();
113+
// can't use console.log because that just dispatches to React-JS thread, which might be blocked.
114+
global._log(`UI: Run #${i}: ${r.result} (took ${r.duration}ms)`);
115+
}
116+
runOnJS(setIsBenchmarking)(false);
117+
})();
118+
break;
119+
}
120+
}
121+
},
122+
[]
123+
);
124+
56125
React.useEffect(() => {
57126
const parsedInput = Number.parseInt(input, 10);
58127
run(parsedInput);
@@ -72,6 +141,22 @@ export default function App() {
72141
) : (
73142
<Text>Fibonacci Number: {result}</Text>
74143
)}
144+
145+
<View style={styles.bottom}>
146+
<Button
147+
title="Run heavy calculation on React-JS Thread"
148+
onPress={() => runBenchmark('react')}
149+
/>
150+
<Button
151+
title="Run heavy calculation on separate Thread"
152+
onPress={() => runBenchmark('custom')}
153+
/>
154+
<Button
155+
title="Run heavy calculation on REA UI Thread"
156+
onPress={() => runBenchmark('ui')}
157+
/>
158+
{isBenchmarking && <ActivityIndicator />}
159+
</View>
75160
</View>
76161
);
77162
}
@@ -92,4 +177,7 @@ const styles = StyleSheet.create({
92177
textAlign: 'center',
93178
fontSize: 14,
94179
},
180+
bottom: {
181+
marginTop: 50,
182+
},
95183
});

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@
9999
"root": true,
100100
"globals": {
101101
"_setGlobalConsole": true,
102-
"_LABEL": true
102+
"_LABEL": true,
103+
"performance": true
103104
},
104105
"extends": [
105106
"@react-native-community",

0 commit comments

Comments
 (0)