Skip to content

Commit 1c6a44d

Browse files
committed
cached this.content and this.socreFunction ( faster 7.5%), and have swap default value of bubbleUp an int (faster 80%)
1 parent ef5ec96 commit 1c6a44d

File tree

1 file changed

+45
-34
lines changed

1 file changed

+45
-34
lines changed

astar.js

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -283,34 +283,37 @@ function BinaryHeap(scoreFunction) {
283283

284284
BinaryHeap.prototype = {
285285
push: function(element) {
286+
var content = this.content;
286287
// Add the new element to the end of the array.
287-
this.content.push(element);
288+
content.push(element);
288289

289290
// Allow it to sink down.
290-
this.sinkDown(this.content.length - 1);
291+
this.sinkDown(content.length - 1);
291292
},
292293
pop: function() {
294+
var content = this.content;
293295
// Store the first element so we can return it later.
294-
var result = this.content[0];
296+
var result = content[0];
295297
// Get the element at the end of the array.
296-
var end = this.content.pop();
298+
var end = content.pop();
297299
// If there are any elements left, put the end element at the
298300
// start, and let it bubble up.
299-
if (this.content.length > 0) {
300-
this.content[0] = end;
301+
if (content.length !== 0 ) {
302+
content[0] = end;
301303
this.bubbleUp(0);
302304
}
303305
return result;
304306
},
305307
remove: function(node) {
306-
var i = this.content.indexOf(node);
308+
var content = this.content;
309+
var i = content.indexOf(node);
307310

308311
// When it is found, the process seen in 'pop' is repeated
309312
// to fill up the hole.
310-
var end = this.content.pop();
313+
var end = content.pop();
311314

312-
if (i !== this.content.length - 1) {
313-
this.content[i] = end;
315+
if (i !== content.length - 1) {
316+
content[i] = end;
314317

315318
if (this.scoreFunction(end) < this.scoreFunction(node)) {
316319
this.sinkDown(i);
@@ -326,19 +329,22 @@ BinaryHeap.prototype = {
326329
this.sinkDown(this.content.indexOf(node));
327330
},
328331
sinkDown: function(n) {
332+
var content = this.content;
333+
var scoreFunction = this.scoreFunction;
329334
// Fetch the element that has to be sunk.
330-
var element = this.content[n];
331-
335+
var element = content[n];
336+
//
337+
var elemScore = scoreFunction(element);
338+
var parentN = 0, parent = 0;
332339
// When at 0, an element can not sink any further.
333340
while (n > 0) {
334-
335341
// Compute the parent element's index, and fetch it.
336-
var parentN = ((n + 1) >> 1) - 1;
337-
var parent = this.content[parentN];
342+
parentN = ((n + 1) >> 1) - 1;
343+
parent = content[parentN];
338344
// Swap the elements if the parent is greater.
339-
if (this.scoreFunction(element) < this.scoreFunction(parent)) {
340-
this.content[parentN] = element;
341-
this.content[n] = parent;
345+
if (elemScore < scoreFunction(parent)) {
346+
content[parentN] = element;
347+
content[n] = parent;
342348
// Update 'n' to continue at the new position.
343349
n = parentN;
344350
}
@@ -349,23 +355,29 @@ BinaryHeap.prototype = {
349355
}
350356
},
351357
bubbleUp: function(n) {
358+
var content = this.content;
359+
var scoreFunction = this.scoreFunction;
352360
// Look up the target element and its score.
353-
var length = this.content.length;
354-
var element = this.content[n];
355-
var elemScore = this.scoreFunction(element);
361+
var length = content.length;
362+
var element = content[n];
363+
var elemScore = scoreFunction(element);
364+
// early declarations with type hints
365+
var child2N = 0, child1N = 0;
366+
var child1Score=0;
367+
var swap = -1 ; // no type change !! -1 stands for no swap. X2 speed increase !!!
368+
var child1=null, child2 = null;
356369

357370
while (true) {
358371
// Compute the indices of the child elements.
359-
var child2N = (n + 1) << 1;
360-
var child1N = child2N - 1;
372+
child2N = (n + 1) << 1;
373+
child1N = child2N - 1;
361374
// This is used to store the new position of the element, if any.
362-
var swap = null;
363-
var child1Score;
375+
swap = -1 ;
364376
// If the first child exists (is inside the array)...
365377
if (child1N < length) {
366378
// Look it up and compute its score.
367-
var child1 = this.content[child1N];
368-
child1Score = this.scoreFunction(child1);
379+
child1 = content[child1N];
380+
child1Score = scoreFunction(child1);
369381

370382
// If the score is less than our element's, we need to swap.
371383
if (child1Score < elemScore) {
@@ -375,17 +387,16 @@ BinaryHeap.prototype = {
375387

376388
// Do the same checks for the other child.
377389
if (child2N < length) {
378-
var child2 = this.content[child2N];
379-
var child2Score = this.scoreFunction(child2);
380-
if (child2Score < (swap === null ? elemScore : child1Score)) {
390+
child2 = content[child2N];
391+
if ( scoreFunction(child2) < (swap >= 0 ? elemScore : child1Score)) {
381392
swap = child2N;
382393
}
383394
}
384395

385396
// If the element needs to be moved, swap it, and continue.
386-
if (swap !== null) {
387-
this.content[n] = this.content[swap];
388-
this.content[swap] = element;
397+
if (swap >= 0 ) {
398+
content[n] = content[swap];
399+
content[swap] = element;
389400
n = swap;
390401
}
391402
// Otherwise, we are done.
@@ -401,4 +412,4 @@ return {
401412
Graph: Graph
402413
};
403414

404-
});
415+
});

0 commit comments

Comments
 (0)