mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-11-08 10:09:16 +00:00
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:
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)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user