From 3c50404b46795348dc44b5c8a6291f8d9ba5983a Mon Sep 17 00:00:00 2001 From: Alexander Akait Date: Fri, 23 Oct 2020 18:35:02 +0300 Subject: [PATCH] fix: improved performance (only node@12 and above) (#344) --- src/index.js | 5 ++++- test/parallel-option.test.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 18ca6179..ee78f3f2 100644 --- a/src/index.js +++ b/src/index.js @@ -126,7 +126,10 @@ class TerserPlugin { concurrency = numWorkers; - worker = new Worker(require.resolve('./minify'), { numWorkers }); + worker = new Worker(require.resolve('./minify'), { + numWorkers, + enableWorkerThreads: true, + }); // https://github.com/facebook/jest/issues/8872#issuecomment-524822081 const workerStdout = worker.getStdout(); diff --git a/test/parallel-option.test.js b/test/parallel-option.test.js index 7ccc55b3..00f43f2c 100644 --- a/test/parallel-option.test.js +++ b/test/parallel-option.test.js @@ -27,6 +27,11 @@ jest.mock('os', () => { let workerTransform; let workerEnd; +const ENABLE_WORKER_THREADS = + typeof process.env.ENABLE_WORKER_THREADS !== 'undefined' + ? process.env.ENABLE_WORKER_THREADS === 'true' + : true; + jest.mock('jest-worker', () => { return jest.fn().mockImplementation((workerPath) => { return { @@ -67,6 +72,7 @@ describe('parallel option', () => { expect(Worker).toHaveBeenCalledTimes(1); expect(Worker).toHaveBeenLastCalledWith(workerPath, { + enableWorkerThreads: ENABLE_WORKER_THREADS, numWorkers: os.cpus().length - 1, }); expect(workerTransform).toHaveBeenCalledTimes( @@ -98,6 +104,7 @@ describe('parallel option', () => { expect(Worker).toHaveBeenCalledTimes(1); expect(Worker).toHaveBeenLastCalledWith(workerPath, { + enableWorkerThreads: ENABLE_WORKER_THREADS, numWorkers: Math.min(4, os.cpus().length - 1), }); expect(workerTransform).toHaveBeenCalledTimes( @@ -117,6 +124,7 @@ describe('parallel option', () => { expect(Worker).toHaveBeenCalledTimes(1); expect(Worker).toHaveBeenLastCalledWith(workerPath, { + enableWorkerThreads: ENABLE_WORKER_THREADS, numWorkers: 2, }); expect(workerTransform).toHaveBeenCalledTimes( @@ -140,6 +148,7 @@ describe('parallel option', () => { expect(Worker).toHaveBeenCalledTimes(1); expect(Worker).toHaveBeenLastCalledWith(workerPath, { + enableWorkerThreads: ENABLE_WORKER_THREADS, numWorkers: Math.min(1, os.cpus().length - 1), }); expect(workerTransform).toHaveBeenCalledTimes( @@ -167,6 +176,7 @@ describe('parallel option', () => { expect(Worker).toHaveBeenCalledTimes(1); expect(Worker).toHaveBeenLastCalledWith(workerPath, { + enableWorkerThreads: ENABLE_WORKER_THREADS, numWorkers: Math.min(Object.keys(entries).length, os.cpus().length - 1), }); expect(workerTransform).toHaveBeenCalledTimes( @@ -194,6 +204,7 @@ describe('parallel option', () => { expect(Worker).toHaveBeenCalledTimes(1); expect(Worker).toHaveBeenLastCalledWith(workerPath, { + enableWorkerThreads: ENABLE_WORKER_THREADS, numWorkers: Math.min(Object.keys(entries).length, os.cpus().length - 1), }); expect(workerTransform).toHaveBeenCalledTimes( @@ -232,6 +243,7 @@ describe('parallel option', () => { expect(Worker).toHaveBeenCalledTimes(1); expect(Worker).toHaveBeenLastCalledWith(workerPath, { + enableWorkerThreads: ENABLE_WORKER_THREADS, numWorkers: Math.min(Object.keys(entries).length, os.cpus().length - 1), }); expect(workerTransform).toHaveBeenCalledTimes(