Skip to content

Instantly share code, notes, and snippets.

@yue4u
Created August 18, 2020 15:47
Show Gist options
  • Save yue4u/447c595ed557a5d38b65aab213cebbe7 to your computer and use it in GitHub Desktop.
Save yue4u/447c595ed557a5d38b65aab213cebbe7 to your computer and use it in GitHub Desktop.
const ref = useRef()
const items = useStore((state) => state.items)
const [fast, set] = useState(true)
const Component = fast ? ItemFast : ItemSlow
// Kick off a render-loop which mutates all state objects/each frame
useEffect(() => {
let lastCalledTime = Date.now()
let fps = 0
function renderLoop() {
let delta = (Date.now() - lastCalledTime) / 1000
lastCalledTime = Date.now()
fps = 1 / delta
ref.current.innerText = 'fps ' + fps.toFixed()
useStore.getState().advance()
requestAnimationFrame(renderLoop)
}
renderLoop()
}, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment