forked from nandorojo/moti
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
166 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { default } from './src/Moti.Skeleton' | ||
export { default } from './src/Perf.List' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { useMotify } from 'moti' | ||
import { useEffect, useReducer, useState } from 'react' | ||
import { Button, View, ViewStyle } from 'react-native' | ||
import Animated, { useAnimatedStyle } from 'react-native-reanimated' | ||
|
||
global.shouldDebugMoti = false | ||
|
||
const modes = ['Animated', 'Moti'] as const | ||
|
||
let logs: Record<string, number[]> = {} | ||
|
||
const list = new Array(1).fill(null).map((_, i) => i) | ||
|
||
export default function List() { | ||
const [mode, setMode] = useState<(typeof modes)[number]>(modes[0]) | ||
const [, render] = useReducer((s) => s + 1, 0) | ||
|
||
let renderStartAt = Date.now() | ||
|
||
useEffect(() => { | ||
const interval = setInterval(() => { | ||
render() | ||
|
||
const maxRendersPerMode = 4 | ||
|
||
if (logs[mode]?.length === maxRendersPerMode) { | ||
// remove biggest and smallest values | ||
clearInterval(interval) | ||
const nextMode = modes[modes.indexOf(mode) + 1] | ||
|
||
if (nextMode) { | ||
setMode(nextMode) | ||
} else { | ||
console.log(logs) | ||
|
||
let means = {} | ||
let medians = {} | ||
|
||
for (let key in logs) { | ||
means[key] = logs[key].reduce((a, b) => a + b, 0) / logs[key].length | ||
} | ||
|
||
for (let key in logs) { | ||
const list = logs[key].slice().sort() | ||
medians[key] = list[list.length / 2] ?? list[(list.length - 1) / 2] | ||
} | ||
|
||
console.log('Averages: ', JSON.stringify(means, null, 2)) | ||
|
||
console.log('Medians: ', JSON.stringify(medians, null, 2)) | ||
|
||
logs = {} | ||
} | ||
} | ||
}, 600) | ||
|
||
return () => { | ||
clearInterval(interval) | ||
} | ||
}, [mode]) | ||
|
||
useEffect( | ||
function measurePerf() { | ||
let renderEndAt = Date.now() | ||
|
||
const duration = renderEndAt - renderStartAt | ||
|
||
logs[mode] = logs[mode] || [] | ||
logs[mode].push(duration) | ||
}, | ||
[mode, renderStartAt] | ||
) | ||
|
||
return ( | ||
<View | ||
style={{ | ||
flex: 1, | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
}} | ||
> | ||
{modes.map((m) => ( | ||
<Button | ||
key={m} | ||
title={m} | ||
onPress={() => { | ||
setMode(m) | ||
render() | ||
}} | ||
/> | ||
))} | ||
|
||
<Lists mode={mode} key={Math.random()} /> | ||
</View> | ||
) | ||
} | ||
|
||
const Lists = ({ mode }) => { | ||
return ( | ||
<> | ||
<>{mode === 'Animated' && list.map((_, i) => <Reanimated key={i} />)}</> | ||
<>{mode === 'Moti' && list.map((_, i) => <Moti key={i} />)}</> | ||
</> | ||
) | ||
} | ||
const Reanimated = () => { | ||
return ( | ||
<Animated.View style={useAnimatedStyle(() => ({}), [])}></Animated.View> | ||
) | ||
} | ||
|
||
const Moti = () => { | ||
const { style } = useMotify<ViewStyle>({}) | ||
return <Animated.View style={style}></Animated.View> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,28 @@ | ||
{ | ||
"compilerOptions": {}, | ||
"compilerOptions": { | ||
"paths": { | ||
"moti": ["../../packages/moti/src"] | ||
}, | ||
"composite": true, | ||
"allowUnreachableCode": false, | ||
"allowUnusedLabels": false, | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"jsx": "react-jsx", | ||
"lib": ["esnext", "dom"], | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"noFallthroughCasesInSwitch": true, | ||
"noImplicitReturns": true, | ||
"noImplicitUseStrict": false, | ||
"noStrictGenericChecks": false, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"resolveJsonModule": true, | ||
"skipLibCheck": true, | ||
"strict": true, | ||
"target": "esnext", | ||
"noImplicitAny": false | ||
}, | ||
"extends": "expo/tsconfig.base" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,5 +26,5 @@ | |
"target": "esnext", | ||
"noImplicitAny": false | ||
}, | ||
"exclude": ["examples"] | ||
"exclude": ["./examples"] | ||
} |