@@ -7,9 +7,10 @@ import {
7
7
TextInput ,
8
8
ActivityIndicator ,
9
9
Alert ,
10
+ Button ,
10
11
} from 'react-native' ;
11
12
import { spawnThread } from 'react-native-multithreading' ;
12
- import 'react-native-reanimated' ;
13
+ import { runOnJS , runOnUI } from 'react-native-reanimated' ;
13
14
14
15
const fibonacci = ( num : number ) : number => {
15
16
'worklet' ;
@@ -26,6 +27,7 @@ const fibonacci = (num: number): number => {
26
27
} ;
27
28
28
29
export default function App ( ) {
30
+ const [ isBenchmarking , setIsBenchmarking ] = React . useState ( false ) ;
29
31
const [ isRunning , setIsRunning ] = React . useState ( false ) ;
30
32
const [ input , setInput ] = React . useState ( '5' ) ;
31
33
const [ result , setResult ] = React . useState < number | undefined > ( ) ;
@@ -53,6 +55,73 @@ export default function App() {
53
55
}
54
56
} , [ ] ) ;
55
57
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
+
56
125
React . useEffect ( ( ) => {
57
126
const parsedInput = Number . parseInt ( input , 10 ) ;
58
127
run ( parsedInput ) ;
@@ -72,6 +141,22 @@ export default function App() {
72
141
) : (
73
142
< Text > Fibonacci Number: { result } </ Text >
74
143
) }
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 >
75
160
</ View >
76
161
) ;
77
162
}
@@ -92,4 +177,7 @@ const styles = StyleSheet.create({
92
177
textAlign : 'center' ,
93
178
fontSize : 14 ,
94
179
} ,
180
+ bottom : {
181
+ marginTop : 50 ,
182
+ } ,
95
183
} ) ;
0 commit comments