Last active
April 13, 2021 14:10
-
-
Save shqld/8ea2ac6f4e1342fca8620a96c0f737c3 to your computer and use it in GitHub Desktop.
Event Loop Race
This file contains hidden or 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
race() | |
function race() { | |
let count = 1 | |
function finish(message) { | |
console.log(`${count++}. ${message}`) | |
} | |
setTimeout(() => finish('timeout'), 0) | |
if (typeof postMessage !== 'undefined') { | |
addEventListener( | |
'message', | |
({ data }) => data === 'race' && finish('onmessage'), | |
{ once: true } | |
) | |
postMessage('race') | |
} | |
if (typeof setImmediate !== 'undefined') | |
setImmediate(() => finish('immediate')) | |
if (typeof requestAnimationFrame !== 'undefined') | |
requestAnimationFrame(() => finish('raf')) | |
if (typeof process !== 'undefined') | |
process.nextTick(() => finish('nextTick')) | |
console.log('race has started') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment