| 285 | let bestDxSq = maxDxSq; |
| 286 | |
| 287 | const tryUpdate = (idx: number, dxSq: number) => { |
| 288 | if (!Number.isFinite(dxSq)) return; |
| 289 | const isBetter = |
| 290 | dxSq < bestDxSq || (dxSq === bestDxSq && (bestDataIndex < 0 || idx < bestDataIndex)); |
| 291 | if (!isBetter) return; |
| 292 | bestDxSq = dxSq; |
| 293 | bestDataIndex = idx; |
| 294 | bestPoint = data[idx] as DataPoint; |
| 295 | }; |
| 296 | |
| 297 | // If the series contains NaN x values, binary search cannot be trusted (NaN breaks ordering). |
| 298 | // Fall back to a linear scan for correctness. Cached per data array for performance. |