Skip to content

Instantly share code, notes, and snippets.

@peterbraden
Created August 30, 2010 18:38
Show Gist options
  • Save peterbraden/557826 to your computer and use it in GitHub Desktop.
Save peterbraden/557826 to your computer and use it in GitHub Desktop.
It would be nice to have a browser native delay function that added
a function to the call stack to be run immediately, but not blocking
UI, similar to
> setTimeout(func, 0).
Currently prototype does this - prototype.function.defer sets a
timeout of 0.01.
In my experience though, setting a timeout has a delay - Resig's
experiments seem to confirm:
> http://ejohn.org/blog/analyzing-timer-performance/
It'd be nice if
> window.defer(func)
was isomorphic to
> window.setTimeout(func, 0.01)
so that older browsers could be supported, but code ran much faster
with the defer code.
Is this a good idea? Is there a way to do this currently?
@cameron
Copy link

cameron commented Aug 31, 2010

Mr. Braden,

You're right -- setTime(func, 0) runs between 5-20ms later (or something, depending on browser). Ben posted an article about it similar to Resig's but with updated numbers: http://www.adequatelygood.com/2010/2/Minimum-Timer-Intervals-in-JavaScript

Your basic goal is background processing? I remember seeing something about worker threads coming to an HTML5 implementation near you, but they're not widely supported yet...

http://blog.mozbox.org/post/2009/04/10/Web-Workers-in-action
http://www.ajaxwith.com/JavaScript-Worker-Threads.html

Please enjoy my near-zero contribution to this conversation :)

@peterbraden
Copy link
Author

Mr Boehmer,

Thanks - I hadn't realised Ben had investigated the delay.

I've played with worker threads, and whilst pretty cool, they're a hassle to implement because of the message passing.

The goal with this would be to stop the browser locking up on large loops - I find myself reimplementing the same chunk processing code and then trying to tune the timeout to be as quick as possible. Ideally I could just pass an iterable to a map function, and using this delay function it would ensure that the loop doesn't block the browser's thread.

So not as much background processing as splitting up foreground processing.

P

@peterbraden
Copy link
Author

Mr Boehmer,

Thanks - I hadn't realised Ben had investigated the delay.

I've played with worker threads, and whilst pretty cool, they're a hassle to implement because of the message passing.

The goal with this would be to stop the browser locking up on large loops - I find myself reimplementing the same chunk processing code and then trying to tune the timeout to be as quick as possible. Ideally I could just pass an iterable to a map function, and using this delay function it would ensure that the loop doesn't block the browser's thread.

So not as much background processing as splitting up foreground processing.

P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment