From 2c489378fc0b4e1a92ed87102fa3f79f36710e27 Mon Sep 17 00:00:00 2001 From: Krzysztof Piaskowy Date: Thu, 27 Jan 2022 14:08:47 +0100 Subject: [PATCH] Fix immutable color cache (#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 (