@@ -10,8 +10,10 @@ import {
10
10
Button ,
11
11
} from 'react-native' ;
12
12
import { spawnThread } from 'react-native-multithreading' ;
13
+ import 'react-native-reanimated' ;
13
14
import { runOnJS , runOnUI } from 'react-native-reanimated' ;
14
15
16
+ // calculates the fibonacci number - that can be optimized really good so it's really really fast.
15
17
const fibonacci = ( num : number ) : number => {
16
18
'worklet' ;
17
19
// Uses array to store every single fibonacci number
@@ -26,13 +28,40 @@ const fibonacci = (num: number): number => {
26
28
return fib [ fib . length - 1 ] ;
27
29
} ;
28
30
31
+ // complex branches with modular if so that it cannot be optimized very well. Takes ~4-5 seconds on my i9.
32
+ const benchmark = ( ) => {
33
+ 'worklet' ;
34
+ // global.performance is not yet installed. I will do that soon.
35
+ const start = performance . now ( ) ;
36
+ function p ( n : number ) {
37
+ for ( var i = 2 ; i * i <= n ; i ++ ) {
38
+ if ( n % i === 0 ) {
39
+ return false ;
40
+ }
41
+ }
42
+ return true ;
43
+ }
44
+
45
+ var sum = 0 ;
46
+ for ( var k = 2 ; k < 1000000 ; k ++ ) {
47
+ if ( p ( k ) ) {
48
+ sum ++ ;
49
+ }
50
+ }
51
+ const end = performance . now ( ) ;
52
+ return {
53
+ result : sum ,
54
+ duration : end - start ,
55
+ } ;
56
+ } ;
57
+
29
58
export default function App ( ) {
30
59
const [ isBenchmarking , setIsBenchmarking ] = React . useState ( false ) ;
31
60
const [ isRunning , setIsRunning ] = React . useState ( false ) ;
32
61
const [ input , setInput ] = React . useState ( '5' ) ;
33
62
const [ result , setResult ] = React . useState < number | undefined > ( ) ;
34
63
35
- const run = React . useCallback ( async ( parsedInput : number ) => {
64
+ const runFibonacci = React . useCallback ( async ( parsedInput : number ) => {
36
65
setIsRunning ( true ) ;
37
66
try {
38
67
const fib = await spawnThread ( ( ) => {
@@ -55,38 +84,22 @@ export default function App() {
55
84
}
56
85
} , [ ] ) ;
57
86
87
+ React . useEffect ( ( ) => {
88
+ const parsedInput = Number . parseInt ( input , 10 ) ;
89
+ runFibonacci ( parsedInput ) ;
90
+ } , [ runFibonacci , input ] ) ;
91
+
58
92
const runBenchmark = React . useCallback (
59
93
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
94
setIsBenchmarking ( true ) ;
85
95
switch ( thread ) {
86
96
case 'react' : {
87
97
for ( let i = 0 ; i < 5 ; i ++ ) {
88
98
const r = benchmark ( ) ;
89
- console . log ( `REACT: Run #${ i } : ${ r . result } (took ${ r . duration } ms)` ) ;
99
+ global . nativeLoggingHook (
100
+ `REACT: Run #${ i } : ${ r . result } (took ${ r . duration } ms)` ,
101
+ 1
102
+ ) ;
90
103
}
91
104
setIsBenchmarking ( false ) ;
92
105
break ;
@@ -97,7 +110,7 @@ export default function App() {
97
110
for ( let i = 0 ; i < 5 ; i ++ ) {
98
111
const r = benchmark ( ) ;
99
112
// can't use console.log because that just dispatches to React-JS thread, which might be blocked.
100
- console . log (
113
+ global . _log (
101
114
`CUSTOM: Run #${ i } : ${ r . result } (took ${ r . duration } ms)`
102
115
) ;
103
116
}
@@ -106,27 +119,23 @@ export default function App() {
106
119
break ;
107
120
}
108
121
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
- } ) ( ) ;
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
+ // })();
118
132
break ;
119
133
}
120
134
}
121
135
} ,
122
136
[ ]
123
137
) ;
124
138
125
- React . useEffect ( ( ) => {
126
- const parsedInput = Number . parseInt ( input , 10 ) ;
127
- run ( parsedInput ) ;
128
- } , [ run , input ] ) ;
129
-
130
139
return (
131
140
< View style = { styles . container } >
132
141
< Text > Input:</ Text >
0 commit comments