Skip to content

Commit

Permalink
pointsToCost performance improvement
Browse files Browse the repository at this point in the history
Improve the performance of `pointsToCost` by doing a two-level lookup instead of a single lookup using a concatenated string.
  • Loading branch information
sbj42 committed Mar 31, 2017
1 parent befb8c6 commit 77e6140
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/easystar.js
Expand Up @@ -119,7 +119,10 @@ EasyStar.js = function() {
* @param {Number} The multiplicative cost associated with the given point. * @param {Number} The multiplicative cost associated with the given point.
**/ **/
this.setAdditionalPointCost = function(x, y, cost) { this.setAdditionalPointCost = function(x, y, cost) {
pointsToCost[x + '_' + y] = cost; if (pointsToCost[y]===undefined) {
pointsToCost[y] = {};
}
pointsToCost[y][x] = cost;
}; };


/** /**
Expand All @@ -129,7 +132,9 @@ EasyStar.js = function() {
* @param {Number} y The y value of the point to stop costing. * @param {Number} y The y value of the point to stop costing.
**/ **/
this.removeAdditionalPointCost = function(x, y) { this.removeAdditionalPointCost = function(x, y) {
delete pointsToCost[x + '_' + y]; if (pointsToCost[y]) {
delete pointsToCost[y][x];
}
} }


/** /**
Expand Down Expand Up @@ -491,7 +496,7 @@ EasyStar.js = function() {
}; };


var getTileCost = function(x, y) { var getTileCost = function(x, y) {
return pointsToCost[x + '_' + y] || costMap[collisionGrid[y][x]] return (pointsToCost[y] && pointsToCost[y][x]) || costMap[collisionGrid[y][x]]
}; };


var coordinateToNode = function(instance, x, y, parent, cost) { var coordinateToNode = function(instance, x, y, parent, cost) {
Expand Down

0 comments on commit 77e6140

Please sign in to comment.