Skip to content

Commit df792e3

Browse files
committed
Move pathTo function outside astar.search
This small optimization avoid to redefine the pathTo function in memory each time astar.search() is called.
1 parent f812ecc commit df792e3

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

astar.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717
}
1818
})(function() {
1919

20+
function pathTo(node){
21+
var curr = node,
22+
path = [];
23+
while(curr.parent) {
24+
path.push(curr);
25+
curr = curr.parent;
26+
}
27+
return path.reverse();
28+
}
29+
2030
var astar = {
2131
init: function(graph) {
2232
for (var i = 0, len = graph.nodes.length; i < len; ++i) {
@@ -56,17 +66,6 @@ var astar = {
5666

5767
start.h = heuristic(start, end);
5868

59-
function pathTo(node){
60-
var curr = node;
61-
var path = [];
62-
while(curr.parent) {
63-
path.push(curr);
64-
curr = curr.parent;
65-
}
66-
return path.reverse();
67-
}
68-
69-
7069
openHeap.push(start);
7170

7271
while(openHeap.size() > 0) {

0 commit comments

Comments
 (0)