pavelbabenko / react-native-awesome-gallery

Performant, native-like, and well-customizable gallery for React Native.
MIT License
495 stars 54 forks source link

Replace 2 loops by one #59

Closed mykyta-rusyn closed 11 months ago

mykyta-rusyn commented 11 months ago

In the snapPoint function we have two loops - that should find the minimum delta. This function looks good, but we can change it to one loop, with the same functionality. From:

const deltas = points.map((p) => Math.abs(point - p));
const minDelta = Math.min.apply(null, deltas);

To:

let minDelta = Number.MAX_VALUE;
for (let i = 0; i < points.length; i++) {
  const newDelta = Math.abs(point - points[i]);
  if (newDelta < minDelta) {
    minDelta = newDelta;
  }
}
pavelbabenko commented 11 months ago

@mykyta-rusyn The function doesn't return minDelta. It returns a point with minimum delta. If you find a solution which returns point with minimum delta with a single loop, feel free to create a PR