uid-pool
Version:
High-performance UUID and unique ID pooling for Node.js. Pre-generate IDs in background worker threads for O(1) synchronous acquisition. Drop-in replacement for uuid.v4() and nanoid() with 10-100x better performance under load.
34 lines • 1.19 kB
JavaScript
import { parentPort } from "worker_threads";
if (!parentPort) {
throw new Error("This file must be run as a worker thread");
}
// Listen for messages from the main thread
parentPort.on("message", async (message) => {
if (message.type === "GENERATE") {
try {
// Create the generator function from the serialized code
const generator = new Function("return " + message.generatorCode)();
const ids = [];
for (let i = 0; i < message.count; i++) {
const id = await generator();
ids.push(id);
}
const response = {
type: "GENERATED",
ids,
};
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
parentPort.postMessage(response);
}
catch (error) {
const response = {
type: "GENERATED",
ids: [],
error: error.message,
};
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
parentPort.postMessage(response);
}
}
});
//# sourceMappingURL=worker.js.map