Skip to content

Commit abcc244

Browse files
committed
2 parents 2a1cb63 + df792e3 commit abcc244

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

astar.js

Lines changed: 17 additions & 18 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) {
@@ -224,18 +223,18 @@ Graph.prototype.neighbors = function(node) {
224223
};
225224

226225
Graph.prototype.toString = function() {
227-
var graphString = "\n";
228-
var nodes = this.nodes;
229-
var rowDebug, row, y, l;
226+
var graphString = [],
227+
nodes = this.grid, // when using grid
228+
rowDebug, row, y, l;
230229
for (var x = 0, len = nodes.length; x < len; x++) {
231-
rowDebug = "";
230+
rowDebug = [];
232231
row = nodes[x];
233232
for (y = 0, l = row.length; y < l; y++) {
234-
rowDebug += row[y].weight + " ";
233+
rowDebug.push(row[y].weight);
235234
}
236-
graphString = graphString + rowDebug + "\n";
235+
graphString.push(rowDebug.join(" "));
237236
}
238-
return graphString;
237+
return graphString.join("\n");
239238
};
240239

241240
function GridNode(x, y, weight) {

benchmark/benchmark.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ $(function() {
1313
var times = 0;
1414

1515
for (var i = 0; i < 1000; i++) {
16-
var startTime = new Date().getTime();
16+
var startTime = performance ? performance.now() : new Date().getTime();
1717
var result = astar.search(graph, start, end);
18-
var endTime = new Date().getTime();
18+
var endTime = performance ? performance.now() : new Date().getTime();
1919
times = times + (endTime - startTime);
2020

2121
results.push(
2222
'<li>Found path with ' + result.length + ' steps. ' +
23-
'Took ' + (endTime - startTime) + ' milliseconds.</li>'
23+
'Took ' + (endTime - startTime).toFixed(2) + ' milliseconds.</li>'
2424
);
2525
}
2626

2727
$("#graph").html(graph.toString());
28-
$("#summary").html('Average time: ' + (times / 1000) + 'ms');
28+
$("#summary").html('Average time: ' + (times / 1000).toFixed(2) + 'ms');
2929
$("#results").html(results.join(''));
3030

3131
running = false;

demo/demo.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,19 @@ GraphSearch.prototype.cellClicked = function($end) {
179179
var $start = this.$cells.filter("." + css.start);
180180
var start = this.nodeFromElement($start);
181181

182-
var sTime = new Date();
182+
var sTime = performance ? performance.now() : new Date().getTime();
183183
var path = this.search(this.graph, start, end, {
184184
closest: this.opts.closest
185185
});
186-
var fTime = new Date();
186+
var fTime = performance ? performance.now() : new Date().getTime();
187+
var duration = (fTime-sTime).toFixed(2);
187188

188189
if(!path || path.length == 0) {
189-
$("#message").text("couldn't find a path ("+(fTime-sTime)+"ms)");
190+
$("#message").text("couldn't find a path (" + duration + "ms)");
190191
this.animateNoPath();
191192
}
192193
else {
193-
$("#message").text("search took " + (fTime-sTime) + "ms.");
194+
$("#message").text("search took " + duration + "ms.");
194195
if(this.opts.debug) {
195196
this.drawDebugInfo(this.opts.debug);
196197
}

0 commit comments

Comments
 (0)