diff --git a/lib/internal/priority_queue.js b/lib/internal/priority_queue.js index 2f1db934b43cc8..6b7f828676f8a9 100644 --- a/lib/internal/priority_queue.js +++ b/lib/internal/priority_queue.js @@ -77,6 +77,24 @@ module.exports = class PriorityQueue { setPosition(item, pos); } + updateAt(pos) { + const heap = this[kHeap]; + const item = heap[pos]; + this.removeAt(pos); + this.insert(item); + } + + update(value) { + const heap = this[kHeap]; + const pos = heap.indexOf(value); + if (pos < 1) + return false; + + this.updateAt(pos); + + return true; + } + removeAt(pos) { const heap = this[kHeap]; const size = --this[kSize]; diff --git a/lib/timers.js b/lib/timers.js index 575dcf25f6e481..68381d90ec807e 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -285,7 +285,7 @@ function listOnTimeout(list, now) { if (diff < msecs) { list.expiry = timer._idleStart + msecs; list.id = timerListId++; - queue.percolateDown(1); + queue.updateAt(list.priorityQueuePosition); debug('%d list wait because diff is %d', msecs, diff); return; }