Skip to content

Commit

Permalink
Avoid linear scan when adding new timer.
Browse files Browse the repository at this point in the history
Timer callbacks are now assigned a unique id, such that if an existing callback
is later replaced, the timing parameters can be updated using a constant-time
lookup rather than a linear scan of the timer queue.
  • Loading branch information
mbostock committed Oct 4, 2012
1 parent 428ea4f commit 443f0e6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 35 deletions.
20 changes: 7 additions & 13 deletions d3.v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@
var t0 = null, t1 = d3_timer_queue, then = Infinity;
while (t1) {
if (t1.flush) {
delete d3_timer_byId[t1.callback.id];
t1 = t0 ? t0.next = t1.next : d3_timer_queue = t1.next;
} else {
then = Math.min(then, t1.then + t1.delay);
Expand Down Expand Up @@ -4063,24 +4064,17 @@
return typeof b === "function" ? tweenFunction : b == null ? d3_tweenNull : (b += "", tweenString);
};
var d3_tweenRemove = {};
var d3_timer_queue = null, d3_timer_interval, d3_timer_timeout;
var d3_timer_id = 0, d3_timer_byId = {}, d3_timer_queue = null, d3_timer_interval, d3_timer_timeout;
d3.timer = function(callback, delay, then) {
var found = false, t0, t1 = d3_timer_queue;
if (arguments.length < 3) {
if (arguments.length < 2) delay = 0; else if (!isFinite(delay)) return;
then = Date.now();
}
while (t1) {
if (t1.callback === callback) {
t1.then = then;
t1.delay = delay;
found = true;
break;
}
t0 = t1;
t1 = t1.next;
}
if (!found) d3_timer_queue = {
var timer = d3_timer_byId[callback.id];
if (timer && timer.callback === callback) {
timer.then = then;
timer.delay = delay;
} else d3_timer_byId[callback.id = ++d3_timer_id] = d3_timer_queue = {
callback: callback,
then: then,
delay: delay,
Expand Down
Loading

0 comments on commit 443f0e6

Please sign in to comment.