Skip to content

Commit f812ecc

Browse files
committed
Use Performance API if available
Performance API is more precise https://developer.mozilla.org/en-US/docs/Web/API/Performance http://updates.html5rocks.com/2012/08/When-milliseconds-are-not-enough-performance-now Note: I did not switch new Date().getTime() to Date.now() because it only works with IE>8. But this might be interesting because Date.now() is faster than new Date().getTime() (no object creation). http://stackoverflow.com/questions/12517359/performance-date-now-vs-date-gettime
1 parent 44882f7 commit f812ecc

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

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)