diff --git a/packages/vitest/src/node/pool.ts b/packages/vitest/src/node/pool.ts index ce89e958bb57..2873c5ed82f2 100644 --- a/packages/vitest/src/node/pool.ts +++ b/packages/vitest/src/node/pool.ts @@ -1,5 +1,6 @@ import { MessageChannel } from 'worker_threads' import { pathToFileURL } from 'url' +import { cpus } from 'os' import { resolve } from 'pathe' import type { Options as TinypoolOptions } from 'tinypool' import { Tinypool } from 'tinypool' @@ -55,17 +56,20 @@ export function createFakePool(ctx: Vitest): WorkerPool { } export function createWorkerPool(ctx: Vitest): WorkerPool { + const threadsCount = ctx.config.watch + ? Math.max(cpus().length / 2, 1) + : Math.max(cpus().length - 1, 1) + const options: TinypoolOptions = { filename: workerPath, // Disable this for now, for WebContainer capability // https://github.com/vitest-dev/vitest/issues/93 // In future we could conditionally enable it based on the env useAtomics: false, + + maxThreads: ctx.config.maxThreads ?? threadsCount, + minThreads: ctx.config.minThreads ?? threadsCount, } - if (ctx.config.maxThreads != null) - options.maxThreads = ctx.config.maxThreads - if (ctx.config.minThreads != null) - options.minThreads = ctx.config.minThreads if (ctx.config.isolate) { options.isolateWorkers = true options.concurrentTasksPerWorker = 1