Skip to content

Commit

Permalink
perf: Fix unecessary lut generation with memoization
Browse files Browse the repository at this point in the history
  • Loading branch information
Undistraction committed Jul 29, 2024
1 parent 76f376a commit b39110b
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/utils/interpolate/even.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import memoize from 'fast-memoize'
import { times, timesReduce } from '../functional'
import { getDistanceBetweenPoints, roundTo10 } from '../math'
import { validateT } from '../validation'
Expand Down Expand Up @@ -48,6 +49,13 @@ const findClosestPointOnCurve = (lut, curve, targetLength, precision) => {
}
}

// We only want to do this once per curve as it is very expensive
const getLutForCurve = memoize((curve, precision) => {
console.log(curve)
const pointsApproximate = getApproximatePointsOnCurve(curve, precision)
return getLut(pointsApproximate)
})

// -----------------------------------------------------------------------------
// Exports
// -----------------------------------------------------------------------------
Expand All @@ -65,11 +73,7 @@ export const interpolatePointOnCurveEvenlySpaced =

validateT(ratioRounded)

// Approximate the curve with a high number of points
const points = getApproximatePointsOnCurve(curve, precision)

// Calculate the cumulative arc length
const lut = getLut(points)
const lut = getLutForCurve(curve, precision)

const totalLength = lut[lut.length - 1]
const targetLength = ratioRounded * totalLength
Expand Down

0 comments on commit b39110b

Please sign in to comment.