forked from software-mansion/react-native-reanimated
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix immutable color cache (software-mansion#2796)
## Description The color cache was mutable only from the JS side. I decided to remove the cache and instead of this I created a new faster function with a better cache mechanism without previous limitations. Performance comparation between `interpolateColor` and `interpolateSharableColor`: ``` old 2894ms - new 1741ms // 39.8% faster old 2720ms - new 1697ms // 37.6% faster old 2739ms - new 1650ms // 39.7% faster old 2738ms - new 1781ms // 34.9% faster old 2727ms - new 1662ms // 39.0% faster ``` Performance test code: ```js const interpolateConfig = useInterpolateConfig([0, 350], ['#ff0000', '#00ff00']); const v1 = interpolateSharableColor(randomWidth.value, interpolateConfig); const iterations = 10000; const t1_start = Date.now(); for(let i = 0; i < iterations; i++) { const v = interpolateSharableColor(randomWidth.value, interpolateConfig); } const t1_end = Date.now(); const t2_start = Date.now(); for(let i = 0; i < iterations; i++) { const v = interpolateColor(randomWidth.value, [0, 350], ['#ff0000', '#00ff00']); } const t2_end = Date.now(); console.log(t1_end - t1_start, t2_end - t2_start); ``` ## Usage ```js import Animated, { useSharedValue, withTiming, useAnimatedStyle, interpolateSharableColor, useInterpolateConfig, } from 'react-native-reanimated'; import { View, Button } from 'react-native'; import React from 'react'; function AnimatedStyleUpdateExample(): React.ReactElement { const randomWidth = useSharedValue(10); const interpolateConfig = useInterpolateConfig([0, 350], ['#ff0000', '#00ff00']); // <- here const style = useAnimatedStyle(() => { return { width: randomWidth.value, backgroundColor: interpolateSharableColor(randomWidth.value, interpolateConfig) // <- here }; }); return ( <View style={{ flex: 1, flexDirection: 'column', }}> <Animated.View style={[ { width: 100, height: 80, backgroundColor: 'black', margin: 30 }, style ]} /> <Button title="toggle" onPress={() => { randomWidth.value = withTiming(Math.random() * 350) }} /> </View> ); } export default AnimatedStyleUpdateExample; ``` Fixes software-mansion#2329 software-mansion#2739
- Loading branch information
Showing
2 changed files
with
113 additions
and
54 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
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