Skip to content

🎥 Make videos programmatically with React

`remotion`: Cache measureSpring() values for better performance

Jonny Burger46ac374ebedb
diff --git a/packages/core/src/spring/index.ts b/packages/core/src/spring/index.ts
index 203c39b5a4..57c975829a 100644
--- a/packages/core/src/spring/index.ts
+++ b/packages/core/src/spring/index.ts
@@ -76,17 +76,24 @@ export function spring({
},
};
- const frame =
- (reverse
- ? (passedDurationInFrames ?? naturalDurationGetter.get()) - passedFrame
- : passedFrame) - (reverse ? -delay : delay);
+ const reverseProcessed = reverse
+ ? (passedDurationInFrames ?? naturalDurationGetter.get()) - passedFrame
+ : passedFrame;
+
+ const delayProcessed = reverseProcessed + (reverse ? delay : -delay);
+
+ const durationProcessed =
+ passedDurationInFrames === undefined
+ ? delayProcessed
+ : delayProcessed / (passedDurationInFrames / naturalDurationGetter.get());
+
+ if (passedDurationInFrames && durationProcessed > passedDurationInFrames) {
+ return to;
+ }
const spr = springCalculation({
fps,
- frame:
- passedDurationInFrames === undefined
- ? frame
- : frame / (passedDurationInFrames / naturalDurationGetter.get()),
+ frame: durationProcessed,
config,
from,
to,
diff --git a/packages/core/src/spring/measure-spring.ts b/packages/core/src/spring/measure-spring.ts
index 0bef1e1289..caad39d478 100644
--- a/packages/core/src/spring/measure-spring.ts
+++ b/packages/core/src/spring/measure-spring.ts
@@ -2,6 +2,8 @@ import {validateFps} from '../validation/validate-fps.js';
import type {SpringConfig} from './spring-utils.js';
import {springCalculation} from './spring-utils.js';
+const cache = new Map<string, number>();
+
/**
* @description The function returns how long it takes for a spring animation to settle
* @see [Documentation](https://www.remotion.dev/docs/measure-spring)
@@ -45,6 +47,20 @@ export function measureSpring({
throw new TypeError('Threshold is below 0');
}
+ const cacheKey = [
+ fps,
+ config.damping,
+ config.mass,
+ config.overshootClamping,
+ config.stiffness,
+ from,
+ to,
+ threshold,
+ ].join('-');
+ if (cache.has(cacheKey)) {
+ return cache.get(cacheKey)!;
+ }
+
validateFps(fps, 'to the measureSpring() function', false);
const range = Math.abs(from - to);
@@ -89,5 +105,7 @@ export function measureSpring({
}
}
+ cache.set(cacheKey, finishedFrame);
+
return finishedFrame;
}
diff --git a/packages/core/src/test/spring-bench.test.ts b/packages/core/src/test/spring-bench.test.ts
new file mode 100644
index 0000000000..394be22838
--- /dev/null
+++ b/packages/core/src/test/spring-bench.test.ts
@@ -0,0 +1,32 @@
+import {expect, test} from 'vitest';
+import {spring} from '../spring';
+
+test('Springs should calculate fast and cache the natural duration', async () => {
+ const date = performance.now();
+ for (let i = 0; i < 100000; i++) {
+ spring({
+ frame: 8000,
+ fps: 60,
+ delay: 12000,
+ durationInFrames: Math.round(Math.random() * 100) + 1,
+ });
+ spring({
+ frame: 8000,
+ fps: 60,
+ durationInFrames: Math.round(Math.random() * 100) + 1,
+ });
+ spring({
+ frame: 8000,
+ fps: 60,
+ reverse: true,
+ durationInFrames: Math.round(Math.random() * 100) + 1,
+ });
+ }
+
+ const time = await new Promise((resolve) => {
+ setTimeout(() => {
+ resolve(performance.now() - date);
+ });
+ });
+ expect(time).toBeLessThan(400);
+});