Created
August 15, 2025 13:56
-
-
Save kriszyp/87a5957faffd67136afa57003978742c to your computer and use it in GitHub Desktop.
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
| import { isMainThread,Worker } from 'worker_threads'; | |
| import cluster from 'node:cluster'; | |
| import process from 'node:process'; | |
| let str = 'test'.repeat(100_000); | |
| const numThreads = 32; // a high thread count results in a non-linear slow down | |
| const useProcesses = false; // threads seem to have more contention and are slower | |
| const count = 10000; // how long of test do you want | |
| if (isMainThread && cluster.isPrimary) { | |
| for (let i = 0; i < numThreads; i++) { | |
| if (useProcesses) { | |
| cluster.fork(); | |
| } else { | |
| let worker = new Worker(new URL(import.meta.url)); | |
| } | |
| } | |
| } else { | |
| let encoded = Buffer.from(str); | |
| let start = performance.now(); | |
| let results; | |
| let i = 0; | |
| for (; i < count; i++) { | |
| results = encoded.toString(); | |
| } | |
| console.log('serializations per second: ', count * 1000 / (performance.now() - start)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment