Skip to content

Commit a38de70

Browse files
committed
docs: improve readme
1 parent fd5dd06 commit a38de70

1 file changed

Lines changed: 34 additions & 11 deletions

File tree

README.md

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ unsubscribe()
5858

5959
#### React Hooks - JS Thread only
6060

61-
Please be aware that this hook will not update if your JS thread is blocked (0 FPS) because updates are happening only that very same thread.
61+
Please be aware that this hook will not update if your JS thread is blocked (0 FPS) because updates are happening only on that very same thread.
6262

6363
```tsx
6464
import { useFpsJs } from 'react-native-performance-toolkit'
@@ -71,21 +71,25 @@ const SomeComponent = () => {
7171

7272
#### Reanimated Hooks - UI Thread
7373

74-
To avoid issue with not showing 0 FPS it's recommended to use Reanimated based hooks or pre-made components. That's will ensure that value is updated even if the JS thread is blocked.
74+
To avoid the issue with not showing 0 FPS, it's recommended to use Reanimated based hooks or pre-made components. This will ensure the value is updated even if the JS thread is blocked.
7575

7676
```tsx
7777
import { TextInput } from 'react-native'
7878
import { useFpsJsSharedValue } from 'react-native-performance-toolkit'
79-
import Animated, { useAnimatedReaction } from 'react-native-reanimated'
79+
import Animated, {
80+
useAnimatedReaction,
81+
useAnimatedRef,
82+
setNativeProps,
83+
} from 'react-native-reanimated'
8084

8185
const AnimatedTextInput = Animated.createAnimatedComponent(TextInput)
8286

8387
const SomeComponent = () => {
8488
const inputRef = useAnimatedRef<TextInput>()
85-
const uiFps = useFpsJsSharedValue()
89+
const jsFps = useFpsJsSharedValue()
8690

8791
useAnimatedReaction(
88-
() => uiFps.value.toString(),
92+
() => jsFps.value.toString(),
8993
(value) => {
9094
setNativeProps(inputRef, { text: value })
9195
}
@@ -97,14 +101,27 @@ const SomeComponent = () => {
97101

98102
#### Pre-made Reanimated components - UI Thread
99103

100-
For better DX library provides pre-made Reanimated components that runs solely on UI thread.
104+
For better DX, the library provides pre-made Reanimated components that run solely on the UI thread. You can use either the convenience wrappers or the flexible base component:
101105

102106
```tsx
103-
import { UIThreadReanimatedCounter } from 'react-native-performance-toolkit'
107+
import {
108+
JSFpsCounter,
109+
UIFpsCounter,
110+
CpuUsageCounter,
111+
MemoryUsageCounter,
112+
UIThreadReanimatedCounter,
113+
} from 'react-native-performance-toolkit'
104114

105115
const SomeComponent = () => {
106116
return (
107117
<>
118+
{/* Convenience components */}
119+
<JSFpsCounter />
120+
<UIFpsCounter />
121+
<CpuUsageCounter />
122+
<MemoryUsageCounter />
123+
124+
{/* Or use the flexible base component with custom labels */}
108125
<UIThreadReanimatedCounter label="JS FPS" type="js" />
109126
<UIThreadReanimatedCounter label="UI FPS" type="ui" />
110127
<UIThreadReanimatedCounter label="CPU" type="cpu" />
@@ -116,7 +133,7 @@ const SomeComponent = () => {
116133

117134
#### Direct buffer access (advanced usage, experimental)
118135

119-
Some advanced usage might require direct buffer access. For example you might want to use this library in custom native component or you might want to use it in worklet thread. This is experimental and might be changed in the future.
136+
Some advanced usage might require direct buffer access. For example, you might want to use this library in a custom native component or you might want to use it in a worklet thread. This is experimental and might be changed in the future.
120137

121138
```tsx
122139
import {
@@ -144,7 +161,7 @@ console.log('Memory Usage:', getValueFromBuffer(memoryUsageBuffer))
144161

145162
### Access from worklets (advanced usage)
146163

147-
You can also access value from any worklet thread, but to do that you need use (Nitro Modules unboxing function)[https://nitro.margelo.com/docs/worklets]. For more detailed implementation look for [source code of UI Reanimated hooks like `useFpsJsSharedValue`](https://github.com/Nodonisko/react-native-performance-toolkit/blob/main/src/hooks/uiThreadHooks.ts).
164+
You can also access the value from any worklet thread, but to do that you need to use [Nitro Modules unboxing function](https://nitro.margelo.com/docs/worklets). For more detailed implementation look for [source code of UI Reanimated hooks like `useFpsJsSharedValue`](https://github.com/Nodonisko/react-native-performance-toolkit/blob/main/src/hooks/uiThreadHooks.ts).
148165

149166
```tsx
150167
import { useCallback } from 'react'
@@ -220,9 +237,9 @@ const updateFps = useCallback(() => {
220237

221238
#### Low overhead tracking
222239

223-
On Android library is reading values from virtual files like `/proc/stat` for CPU usage and `/proc/smaps_rollup` for memory usage. This is very low overhead and doesn't require any additional permissions.
240+
On Android, the library is reading values from virtual files like `/proc/stat` for CPU usage and `/proc/smaps_rollup` for memory usage. This is very low overhead and doesn't require any additional permissions.
224241

225-
On iOS library is reading values from `task_vm_info`/`rusage` direct kernel call. This is also super very low overhead.
242+
On iOS, the library is reading values from `task_vm_info`/`rusage` direct kernel call. This is also extremely low overhead.
226243

227244
#### Device Refresh Rate
228245

@@ -236,6 +253,12 @@ The library provides two methods for getting refresh rate information:
236253
- Android: Uses `Display.getRefreshRate()` - useful for devices with adaptive refresh rate (e.g., 60/90/120Hz switching)
237254
- iOS: Uses `CADisplayLink.preferredFramesPerSecond` - useful for ProMotion displays that dynamically adjust
238255

256+
#### JS FPS Tracking
257+
258+
You can build JS FPS tracking in plain JS using setTimeout or requestAnimationFrame. But this library is running similar logic in C++ on the same thread as JS, with lower overhead and a few other benefits. For example, we can easily access the value from other threads.
259+
260+
For now, the FPS baseline for JS FPS is always 60 FPS as it doesn't reflect real UI updates but more like "how busy the JS thread is". I am not 100% sure if this is the correct approach and might change it in the future and I am open to suggestions.
261+
239262
## Contributing
240263

241264
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

0 commit comments

Comments
 (0)