visionmedia/mocha — Issue #153: process.nextTick in browser too slow with big suites and debugger
Author: weepy
Created: 2011-12-18 16:08:11+00:00
State: open

Problem is that without setTimeout to clear the stack - the stack can get riduncuously big - which then a real problem to try to use the Web Inspector with break points etc.

From my experiments I found that the following setZeroTimeout was the best of the ones on the web :

// Only add setZeroTimeout to the window object, and hide everything else in a closure.
(function() {

var timeouts = [],
messageName = 'zero-timeout-message';

// Like setTimeout, but only takes a function argument. There's
// no time argument (always zero) and no arguments (you have to
// use a closure).
function setZeroTimeoutPostMessage(fn) {
timeouts.push(fn);
window.postMessage(messageName, '*');
}

function setZeroTimeout(fn) {
setTimeout(fn, 0);
}

function handleMessage(event) {
if (event.source == window && event.data == messageName) {
if (event.stopPropagation) {
event.stopPropagation();
}
if (timeouts.length) {
timeouts.shift()();
}
}
}

if (window.postMessage) {
if (window.addEventListener) {
window.addEventListener('message', handleMessage, true);
} else if (window.attachEvent) {
window.attachEvent('onmessage', handleMessage);
}
window.setZeroTimeout = setZeroTimeoutPostMessage;
} else {
window.setZeroTimeout = setZeroTimeout;
}

}());