Browse Source

Use the timer list for setTimeout

v0.7.4-release
Ryan Dahl 14 years ago
parent
commit
bc47353bbe
  1. 15
      benchmark/settimeout.js
  2. 89
      lib/timers.js

15
benchmark/settimeout.js

@ -0,0 +1,15 @@
console.log("wait...");
var done = 0;
var N = 5000000;
var begin = new Date();
for (var i = 0; i < N; i++) {
setTimeout(function () {
if (++done == N) {
var end = new Date();
console.log("smaller is better");
console.log("startup: %d", start - begin);
console.log("done: %d", end - start);
}
}, 1000);
}
var start = new Date();

89
lib/timers.js

@ -48,9 +48,8 @@ function remove (item) {
// remove a item from its list and place at the end. // remove a item from its list and place at the end.
function append (list, item) { function append (list, item) {
remove(item);
item._idleNext = list._idleNext; item._idleNext = list._idleNext;
item._idleNext._idlePrev = item; list._idleNext._idlePrev = item;
item._idlePrev = list; item._idlePrev = list;
list._idleNext = item; list._idleNext = item;
} }
@ -62,7 +61,7 @@ function insert (item, msecs) {
item._idleStart = new Date(); item._idleStart = new Date();
item._idleTimeout = msecs; item._idleTimeout = msecs;
if (!msecs) return; if (msecs < 0) return;
var list; var list;
@ -90,23 +89,23 @@ function insert (item, msecs) {
return; return;
} else { } else {
remove(first); remove(first);
assert(first != peek(list)); assert(first !== peek(list));
if (first._onTimeout) first._onTimeout(); if (first._onTimeout) first._onTimeout();
} }
} }
debug(msecs + ' list empty'); debug(msecs + ' list empty');
assert(list._idleNext == list); // list is empty assert(list._idleNext === list); // list is empty
list.stop(); list.stop();
}; };
} }
if (list._idleNext == list) { if (list._idleNext === list) {
// if empty (re)start the timer // if empty (re)start the timer
list.again(msecs); list.again(msecs);
} }
append(list, item); append(list, item);
assert(list._idleNext != list); // list is not empty assert(list._idleNext !== list); // list is not empty
} }
@ -141,7 +140,7 @@ exports.enroll = function (item, msecs) {
// it will reset its timeout. // it will reset its timeout.
exports.active = function (item) { exports.active = function (item) {
var msecs = item._idleTimeout; var msecs = item._idleTimeout;
if (msecs) { if (msecs >= 0) {
var list = lists[msecs]; var list = lists[msecs];
if (item._idleNext == item) { if (item._idleNext == item) {
insert(item, msecs); insert(item, msecs);
@ -159,41 +158,83 @@ exports.active = function (item) {
}; };
/*
* DOM-style timers
*/
exports.setTimeout = function (callback, after) {
var timer;
// Timers if (after <= 0) {
function addTimerListener (callback) { // Use the slow case for after == 0
var timer = this; timer = new Timer();
// Special case the no param case to avoid the extra object creation. timer.callback = callback;
} else {
timer = { _idleTimeout: after, _onTimeout: callback };
timer._idlePrev = timer;
timer._idleNext = timer;
}
/*
* Sometimes setTimeout is called with arguments, EG
*
* setTimeout(callback, 2000, "hello", "world")
*
* If that's the case we need to call the callback with
* those args. The overhead of an extra closure is not
* desired in the normal case.
*/
if (arguments.length > 2) { if (arguments.length > 2) {
var args = Array.prototype.slice.call(arguments, 2); var args = Array.prototype.slice.call(arguments, 2);
timer.callback = function () { callback.apply(timer, args); }; var c = function () {
} else { callback.apply(timer, args);
timer.callback = callback; };
if (timer instanceof Timer) {
timer.callback = c;
} else {
timer._onTimeout = c;
}
} }
}
if (timer instanceof Timer) {
timer.start(0, 0);
} else {
exports.active(timer);
}
exports.setTimeout = function (callback, after) {
var timer = new Timer();
addTimerListener.apply(timer, arguments);
timer.start(after, 0);
return timer; return timer;
}; };
exports.clearTimeout = function (timer) {
timer.callback = timer._onTimeout = null;
exports.unenroll(timer);
if (timer instanceof Timer) timer.stop(); // for after === 0
};
exports.setInterval = function (callback, repeat) { exports.setInterval = function (callback, repeat) {
var timer = new Timer(); var timer = new Timer();
addTimerListener.apply(timer, arguments);
if (arguments.length > 2) {
var args = Array.prototype.slice.call(arguments, 2);
timer.callback = function () {
callback.apply(timer, args);
};
} else {
timer.callback = callback;
}
timer.start(repeat, repeat ? repeat : 1); timer.start(repeat, repeat ? repeat : 1);
return timer; return timer;
}; };
exports.clearTimeout = function (timer) {
exports.clearInterval = function (timer) {
if (timer instanceof Timer) { if (timer instanceof Timer) {
timer.callback = null; timer.callback = null;
timer.stop(); timer.stop();
} }
}; };
exports.clearInterval = exports.clearTimeout;

Loading…
Cancel
Save