Skip to content

Instantly share code, notes, and snippets.

@kriszyp
Created August 15, 2025 13:56
Show Gist options
  • Select an option

  • Save kriszyp/87a5957faffd67136afa57003978742c to your computer and use it in GitHub Desktop.

Select an option

Save kriszyp/87a5957faffd67136afa57003978742c to your computer and use it in GitHub Desktop.
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