@@ -11,7 +11,6 @@ import {
11
11
} from 'react-native' ;
12
12
import { spawnThread } from 'react-native-multithreading' ;
13
13
import 'react-native-reanimated' ;
14
- import { runOnJS , runOnUI } from 'react-native-reanimated' ;
15
14
16
15
// calculates the fibonacci number - that can be optimized really good so it's really really fast.
17
16
const fibonacci = ( num : number ) : number => {
@@ -55,8 +54,11 @@ const benchmark = () => {
55
54
} ;
56
55
} ;
57
56
57
+ const BENCHMARK_TIMES = 5 ;
58
+
58
59
export default function App ( ) {
59
- const [ isBenchmarking , setIsBenchmarking ] = React . useState ( false ) ;
60
+ const [ isBenchmarkingCustom , setIsBenchmarkingCustom ] = React . useState ( false ) ;
61
+ const [ isBenchmarkingReact , setIsBenchmarkingReact ] = React . useState ( false ) ;
60
62
const [ isRunning , setIsRunning ] = React . useState ( false ) ;
61
63
const [ input , setInput ] = React . useState ( '5' ) ;
62
64
const [ result , setResult ] = React . useState < number | undefined > ( ) ;
@@ -91,44 +93,45 @@ export default function App() {
91
93
92
94
const runBenchmark = React . useCallback (
93
95
async ( thread : 'react' | 'custom' | 'ui' ) => {
94
- setIsBenchmarking ( true ) ;
96
+ console . log (
97
+ 'Starting Benchmark - Please see native console (Xcode Logs/Android Logcat) for output!'
98
+ ) ;
95
99
switch ( thread ) {
96
100
case 'react' : {
97
- for ( let i = 0 ; i < 5 ; i ++ ) {
101
+ setIsBenchmarkingReact ( true ) ;
102
+ global . nativeLoggingHook (
103
+ `REACT_THREAD: Begin blocking React-JS Thread...` ,
104
+ 1
105
+ ) ;
106
+ for ( let i = 0 ; i < BENCHMARK_TIMES ; i ++ ) {
98
107
const r = benchmark ( ) ;
99
108
global . nativeLoggingHook (
100
- `REACT : Run #${ i } : ${ r . result } (took ${ r . duration } ms)` ,
109
+ `REACT_THREAD : Run #${ i } : ${ r . result } (took ${ r . duration } ms)` ,
101
110
1
102
111
) ;
103
112
}
104
- setIsBenchmarking ( false ) ;
113
+ global . nativeLoggingHook (
114
+ `REACT_THREAD: React-JS Thread unblocked!` ,
115
+ 1
116
+ ) ;
117
+ setIsBenchmarkingReact ( false ) ;
105
118
break ;
106
119
}
107
120
case 'custom' : {
121
+ setIsBenchmarkingCustom ( true ) ;
108
122
await spawnThread ( ( ) => {
109
123
'worklet' ;
110
- for ( let i = 0 ; i < 5 ; i ++ ) {
124
+ _log ( `CUSTOM_THREAD: Begin blocking Custom Thread...` ) ;
125
+ for ( let i = 0 ; i < BENCHMARK_TIMES ; i ++ ) {
111
126
const r = benchmark ( ) ;
112
127
// can't use console.log because that just dispatches to React-JS thread, which might be blocked.
113
- global . _log (
114
- `CUSTOM : Run #${ i } : ${ r . result } (took ${ r . duration } ms)`
128
+ _log (
129
+ `CUSTOM_THREAD : Run #${ i } : ${ r . result } (took ${ r . duration } ms)`
115
130
) ;
116
131
}
132
+ _log ( `CUSTOM_THREAD: Custom Thread unblocked!` ) ;
117
133
} ) ;
118
- setIsBenchmarking ( false ) ;
119
- break ;
120
- }
121
- case 'ui' : {
122
- // couldn't manage to get this working, some weird undefined errors
123
- // runOnUI(() => {
124
- // 'worklet';
125
- // for (let i = 0; i < 5; i++) {
126
- // const r = benchmark();
127
- // // can't use console.log because that just dispatches to React-JS thread, which might be blocked.
128
- // global._log(`UI: Run #${i}: ${r.result} (took ${r.duration}ms)`);
129
- // }
130
- // runOnJS(setIsBenchmarking)(false);
131
- // })();
134
+ setIsBenchmarkingCustom ( false ) ;
132
135
break ;
133
136
}
134
137
}
@@ -156,15 +159,12 @@ export default function App() {
156
159
title = "Run heavy calculation on React-JS Thread"
157
160
onPress = { ( ) => runBenchmark ( 'react' ) }
158
161
/>
162
+ { isBenchmarkingReact && < ActivityIndicator /> }
159
163
< Button
160
164
title = "Run heavy calculation on separate Thread"
161
165
onPress = { ( ) => runBenchmark ( 'custom' ) }
162
166
/>
163
- < Button
164
- title = "Run heavy calculation on REA UI Thread"
165
- onPress = { ( ) => runBenchmark ( 'ui' ) }
166
- />
167
- { isBenchmarking && < ActivityIndicator /> }
167
+ { isBenchmarkingCustom && < ActivityIndicator /> }
168
168
</ View >
169
169
</ View >
170
170
) ;
0 commit comments