Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process.nextTick in browser too slow with big suites and debugger #153

Closed
weepy opened this issue Dec 18, 2011 · 0 comments
Closed

process.nextTick in browser too slow with big suites and debugger #153

weepy opened this issue Dec 18, 2011 · 0 comments
Labels
type: feature enhancement proposal

Comments

@weepy
Copy link

weepy commented Dec 18, 2011

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;
  }

}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature enhancement proposal
Projects
None yet
Development

No branches or pull requests

2 participants