Permalink
Browse files

`pointsToCost` performance improvement

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 77e61409b5a52e02e19c62a93c911174cf4f18dd
Showing with 8 additions and 3 deletions.
  1. +8 −3 src/easystar.js
View
@@ -119,7 +119,10 @@ EasyStar.js = function() {
* @param {Number} The multiplicative cost associated with the given point.
**/
this.setAdditionalPointCost = function(x, y, cost) {
pointsToCost[x + '_' + y] = cost;
if (pointsToCost[y]===undefined) {
pointsToCost[y] = {};
}
pointsToCost[y][x] = cost;
};
/**
@@ -129,7 +132,9 @@ EasyStar.js = function() {
* @param {Number} y The y value of the point to stop costing.
**/
this.removeAdditionalPointCost = function(x, y) {
delete pointsToCost[x + '_' + y];
if (pointsToCost[y]) {
delete pointsToCost[y][x];
}
}
/**
@@ -491,7 +496,7 @@ EasyStar.js = function() {
};
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) {

0 comments on commit 77e6140

Please sign in to comment.