Skip to content

Commit

Permalink
perf: improve (nearestPoint) calculation by 50%+
Browse files Browse the repository at this point in the history
  • Loading branch information
WadhahEssam committed Aug 28, 2023
1 parent a1ec848 commit 39a8f57
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,23 @@ export const getIndexOfTheNearestXPoint = (
) => {
'worklet';

let closest = array[0];
if (!array || array.length === 0) return -1;

let closestIndex = 0;

for (let i = 0; i < array.length; i++) {
while (
closest &&
array[i] &&
// @ts-ignore
Math.abs(array[i].x - value) < Math.abs(closest.x - value)
) {
closest = array[i];
// @ts-ignore
let closestDistance = Math.abs(array[0].x - value);

for (let i = 1; i < array.length; i++) {
// @ts-ignore
const currentDistance = Math.abs(array[i].x - value);

if (currentDistance < closestDistance) {
closestDistance = currentDistance;
closestIndex = i;
}
}

return closestIndex;
};

Expand Down

0 comments on commit 39a8f57

Please sign in to comment.