Created
November 7, 2021 07:32
-
-
Save benolee/88fc68f38493b8ac17bfced293f24efd 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
const workerHandler = `function (e) { | |
if (e.data === 'ping') { | |
self.postMessage('pong') | |
} else { | |
self.setTimeout(function () { | |
self.postMessage('tack') | |
}, 1000/60) | |
} | |
}` | |
const worker = new Worker( | |
window.URL.createObjectURL( | |
new Blob([`self.onmessage=${workerHandler}`]) | |
) | |
) | |
worker.onmessage = ({ data }) => { | |
if (data === 'pong') { | |
worker.postMessage('tick') | |
t0 = performance.now() | |
} else { | |
console.log(performance.now() - t0) | |
} | |
} | |
worker.postMessage('ping') | |
// 16.5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment