From 5e3c629b90b8ab3cc88d5c0571c41a3346a0d0aa Mon Sep 17 00:00:00 2001 From: liabru Date: Sun, 2 May 2021 23:05:21 +0100 Subject: [PATCH] optimised Grid._bucketAddBody --- src/collision/Grid.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/collision/Grid.js b/src/collision/Grid.js index 55b62ef..f7fce43 100644 --- a/src/collision/Grid.js +++ b/src/collision/Grid.js @@ -227,8 +227,13 @@ var Common = require('../core/Common'); * @param {} body */ Grid._bucketAddBody = function(grid, bucket, body) { + var gridPairs = grid.pairs, + pairId = Pair.id, + bucketLength = bucket.length, + i; + // add new pairs - for (var i = 0; i < bucket.length; i++) { + for (i = 0; i < bucketLength; i++) { var bodyB = bucket[i]; if (body.id === bodyB.id || (body.isStatic && bodyB.isStatic)) @@ -236,13 +241,13 @@ var Common = require('../core/Common'); // keep track of the number of buckets the pair exists in // important for Grid.update to work - var pairId = Pair.id(body, bodyB), - pair = grid.pairs[pairId]; + var id = pairId(body, bodyB), + pair = gridPairs[id]; if (pair) { pair[2] += 1; } else { - grid.pairs[pairId] = [body, bodyB, 1]; + gridPairs[id] = [body, bodyB, 1]; } }