-
-
Save mpizenberg/f95c7c84288973b8055388a95bc7ed56 to your computer and use it in GitHub Desktop.
Slow message passing with workers
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
let msgCount = 100000; | |
console.log("Sending", msgCount, " messages ..."); | |
let worker = new Worker(new URL("./deno_worker.mjs", import.meta.url).href, { | |
type: "module", | |
}); | |
worker.onmessage = (msg) => { | |
if (msg.data >= msgCount) { | |
worker.terminate(); | |
Deno.exit(); | |
} else { | |
worker.postMessage(msg.data + 1); | |
} | |
}; | |
worker.postMessage(0); |
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
self.onmessage = (msg) => { | |
self.postMessage(msg.data); | |
}; |
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
import { Worker } from "worker_threads"; | |
let msgCount = 100000; | |
console.log("Sending", msgCount, " messages ..."); | |
let worker = new Worker("./node_worker.mjs", { type: "module" }); | |
worker.on("message", (id) => { | |
if (id > msgCount) { | |
worker.terminate(); | |
process.exit(); | |
} else { | |
worker.postMessage(id + 1); | |
} | |
}); | |
worker.postMessage(0); |
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
import { parentPort } from "worker_threads"; | |
parentPort.on("message", (id) => { | |
parentPort.postMessage(id); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment