JSFiddle - React, Tailwind, and code Playground
HTML
<div id="log"></div>
JavaScript
function log(msg)
{
document.getElementById("log").innerHTML += ("<br/>" + msg);
}
function loop1000(count,yield,cb)
{
if(count++ < 1000)
yield(loop1000,count,yield,cb);
else
cb();
}
function timeTest(yield,cb)
{
var start = Date.now();
loop1000(0,yield,function() {
log("Total time: " + (Date.now() - start));
if(cb)
cb();
});
}
function test1()
{
log("Testing with setTimeout(fn,0)");
var st = function(fn,a,b,c) { setTimeout(fn,0,a,b,c); }
timeTest(st, test2);
}
function test2()
{
log("Testing with soon()");
timeTest(soon);
}
test1();
// See http://www.bluejava.com/4NS/Speed-up-your-Websites-with-a-Faster-setTimeout-using-soon
// Here is our lightening fast setImmediate "shim" - this is MUCH faster than
// setTimeout(fn,0) for yielding threads - its also faster than other setImmediate
// shims, as it uses Mutation Observer and "mainlines" successive calls internally.
// WARNING: This does not yield to the browser UI loop, so by using this repeatedly
// you can starve the UI and be unresponsive to the user.
var soon = (function() {
var fq = []; // function queue;
function callQueue()
{
while(fq.length) // this approach allows new yields to pile on during the execution of these
{
var fe = fq[0];
fe.fn.apply(fe.me,fe.args) // call our fn with the args and preserve context
fq.shift(); // remove element just processed... do this after processing so we don't go 0 and trigger soon again
}
}
// yeild processing and execute this code later - i.e. setImmediate
var fnYield = (function() {
// This is the fastest way browsers have to yield processing
if(typeof MutationObserver !== "undefined")
{
// first, create a div not attached to DOM to "observe"
var dd = document.createElement("div");
return function(fn) {
var mo = new...