Skip to content

Instantly share code, notes, and snippets.

@swannodette
Last active December 18, 2015 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swannodette/5833247 to your computer and use it in GitHub Desktop.
Save swannodette/5833247 to your computer and use it in GitHub Desktop.
var count = 0,
s = null;
equeue = [],
equeue_size = 32;
flush_timer = null,
flush_int = 6, // how often WebKit may fire mouse events
flush_count = 0;
function inc() {
count++;
if(count == 999999) {
console.log("result:", count, ", elapsed ms:", (new Date())-s);
}
}
function queue(f) {
equeue.push(f);
if(equeue.length > equeue_size) {
setTimeout(flush(), 0);
} else if(!flush_timer) {
flush_timer = setTimeout(flush_current, flush_int);
}
}
function flush() {
clearTimeout(flush_timer);
flush_timer = null;
var to_flush = equeue;
equeue = [];
return function() {
exec(to_flush);
};
}
function flush_current() {
flush_timer = null;
exec(equeue);
equeue = [];
}
function exec(xs) {
for(var i = 0; i < xs.length; i++) {
xs[i]();
}
}
function run() {
s = new Date();
for(var i = 0; i < 1000000; i++) {
queue(inc);
}
console.log("queued ms elapsed:", (new Date())-s);
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment