File tree Expand file tree Collapse file tree 3 files changed +26
-26
lines changed Expand file tree Collapse file tree 3 files changed +26
-26
lines changed Original file line number Diff line number Diff line change 17
17
}
18
18
} ) ( function ( ) {
19
19
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
+
20
30
var astar = {
21
31
init : function ( graph ) {
22
32
for ( var i = 0 , len = graph . nodes . length ; i < len ; ++ i ) {
@@ -56,17 +66,6 @@ var astar = {
56
66
57
67
start . h = heuristic ( start , end ) ;
58
68
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
-
70
69
openHeap . push ( start ) ;
71
70
72
71
while ( openHeap . size ( ) > 0 ) {
@@ -224,18 +223,18 @@ Graph.prototype.neighbors = function(node) {
224
223
} ;
225
224
226
225
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 ;
230
229
for ( var x = 0 , len = nodes . length ; x < len ; x ++ ) {
231
- rowDebug = "" ;
230
+ rowDebug = [ ] ;
232
231
row = nodes [ x ] ;
233
232
for ( y = 0 , l = row . length ; y < l ; y ++ ) {
234
- rowDebug += row [ y ] . weight + " " ;
233
+ rowDebug . push ( row [ y ] . weight ) ;
235
234
}
236
- graphString = graphString + rowDebug + "\n" ;
235
+ graphString . push ( rowDebug . join ( " " ) ) ;
237
236
}
238
- return graphString ;
237
+ return graphString . join ( "\n" ) ;
239
238
} ;
240
239
241
240
function GridNode ( x , y , weight ) {
Original file line number Diff line number Diff line change @@ -13,19 +13,19 @@ $(function() {
13
13
var times = 0 ;
14
14
15
15
for ( var i = 0 ; i < 1000 ; i ++ ) {
16
- var startTime = new Date ( ) . getTime ( ) ;
16
+ var startTime = performance ? performance . now ( ) : new Date ( ) . getTime ( ) ;
17
17
var result = astar . search ( graph , start , end ) ;
18
- var endTime = new Date ( ) . getTime ( ) ;
18
+ var endTime = performance ? performance . now ( ) : new Date ( ) . getTime ( ) ;
19
19
times = times + ( endTime - startTime ) ;
20
20
21
21
results . push (
22
22
'<li>Found path with ' + result . length + ' steps. ' +
23
- 'Took ' + ( endTime - startTime ) + ' milliseconds.</li>'
23
+ 'Took ' + ( endTime - startTime ) . toFixed ( 2 ) + ' milliseconds.</li>'
24
24
) ;
25
25
}
26
26
27
27
$ ( "#graph" ) . html ( graph . toString ( ) ) ;
28
- $ ( "#summary" ) . html ( 'Average time: ' + ( times / 1000 ) + 'ms' ) ;
28
+ $ ( "#summary" ) . html ( 'Average time: ' + ( times / 1000 ) . toFixed ( 2 ) + 'ms' ) ;
29
29
$ ( "#results" ) . html ( results . join ( '' ) ) ;
30
30
31
31
running = false ;
Original file line number Diff line number Diff line change @@ -179,18 +179,19 @@ GraphSearch.prototype.cellClicked = function($end) {
179
179
var $start = this . $cells . filter ( "." + css . start ) ;
180
180
var start = this . nodeFromElement ( $start ) ;
181
181
182
- var sTime = new Date ( ) ;
182
+ var sTime = performance ? performance . now ( ) : new Date ( ) . getTime ( ) ;
183
183
var path = this . search ( this . graph , start , end , {
184
184
closest : this . opts . closest
185
185
} ) ;
186
- var fTime = new Date ( ) ;
186
+ var fTime = performance ? performance . now ( ) : new Date ( ) . getTime ( ) ;
187
+ var duration = ( fTime - sTime ) . toFixed ( 2 ) ;
187
188
188
189
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)" ) ;
190
191
this . animateNoPath ( ) ;
191
192
}
192
193
else {
193
- $ ( "#message" ) . text ( "search took " + ( fTime - sTime ) + "ms." ) ;
194
+ $ ( "#message" ) . text ( "search took " + duration + "ms." ) ;
194
195
if ( this . opts . debug ) {
195
196
this . drawDebugInfo ( this . opts . debug ) ;
196
197
}
You can’t perform that action at this time.
0 commit comments