Skip to content

Instantly share code, notes, and snippets.

@benolee
Created November 7, 2021 07:32
Show Gist options
  • Save benolee/88fc68f38493b8ac17bfced293f24efd to your computer and use it in GitHub Desktop.
Save benolee/88fc68f38493b8ac17bfced293f24efd to your computer and use it in GitHub Desktop.
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