Skip to content

Commit 44882f7

Browse files
committed
Efficient Graph.prototype.toString
- Optimize with .join rather than lots of string concatenations - Add comment to specify that .toString() only work in grid layout mode
1 parent 94ff4ce commit 44882f7

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

astar.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,18 +224,18 @@ Graph.prototype.neighbors = function(node) {
224224
};
225225

226226
Graph.prototype.toString = function() {
227-
var graphString = "\n";
228-
var nodes = this.nodes;
229-
var rowDebug, row, y, l;
227+
var graphString = [],
228+
nodes = this.grid, // when using grid
229+
rowDebug, row, y, l;
230230
for (var x = 0, len = nodes.length; x < len; x++) {
231-
rowDebug = "";
231+
rowDebug = [];
232232
row = nodes[x];
233233
for (y = 0, l = row.length; y < l; y++) {
234-
rowDebug += row[y].weight + " ";
234+
rowDebug.push(row[y].weight);
235235
}
236-
graphString = graphString + rowDebug + "\n";
236+
graphString.push(rowDebug.join(" "));
237237
}
238-
return graphString;
238+
return graphString.join("\n");
239239
};
240240

241241
function GridNode(x, y, weight) {

0 commit comments

Comments
 (0)