Last active
December 18, 2015 19:29
-
-
Save swannodette/5833247 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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