Use an indexed loop in getClosestPointTo()

Iterate with an indexed loop over a local reference to the points array
instead of for...of over this.points, which avoids iterator and property
lookup overhead in a loop that can run over thousands of points.
This commit is contained in:
Fabio Pedretti authored and Phosit committed 2026-10-12 16:24:13 +02:00
1 parent 40b8794eec
commit dda1e6c1a3
1 file changed
+5 -3
@@ -25,14 +25,16 @@ Area.prototype.contains = function(point)
Area.prototype.getClosestPointTo = function(position)
{
if (!this.points.length)
const points = this.points;
if (!points.length)
return undefined;
let closestPoint = this.points[0];
let closestPoint = points[0];
let shortestDistance = Infinity;
for (const point of this.points)
for (let i = 0; i < points.length; ++i)
{
const point = points[i];
const currentDistance = point.distanceToSquared(position);
if (currentDistance < shortestDistance)
{