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

Save 1 millisecond! #1334

Closed
wants to merge 1 commit into from
Closed

Save 1 millisecond! #1334

wants to merge 1 commit into from

Conversation

James-Dunn
Copy link
Contributor

The default action of setTimeout will move the function to the end of the call stack. No need to wait one millisecond.

The default action of setTimeout will move the function to the end of the call stack. No need to wait one millisecond.
@oriSomething
Copy link

it won't help, setTimeout at the minimum delay in 4 seconds. the only option to save time is using postMessage

@James-Dunn
Copy link
Contributor Author

It doesn't look like current browsers observe that spec. Testing it on Chrome/Firefox seems to show no minimum for me. http://jsbin.com/foloponuli/edit?html,js,output The word "second" appears first for me if the first timer is set at 2 ms or higher on Chrome, and 1 ms on Firefox.

@oriSomething
Copy link

you don't test it right. the browser will execute setTimeout(..., 1) before setTimeout(..., 2), no matter what the order, but still the first that will run is after 4ms.

@oriSomething
Copy link

edited:
read this

@megawac
Copy link
Contributor

megawac commented Jul 12, 2015

@oriSomething has it right. On another note @jdalton and to derail this a bit, want to consider using setImmediate or process.nextTick where available in v4?

@oriSomething
Copy link

also using MessageChannel / postMessage as a polyfil might be helpful

@jdalton
Copy link
Member

jdalton commented Jul 12, 2015

We avoid a setTimeout of 0 because I believe old Chrome could end up blocking the UI thread with them. I avoid setImmediate because there's no clean way to cancel them with clearTimeout.

There is runInContext which allows you to customize.

var defer = require('lodash').runInContext({ 'setTimeout': setImmediate }).defer;

@lock
Copy link

lock bot commented Dec 27, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Dec 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

Successfully merging this pull request may close these issues.

None yet

4 participants