Skip to content

Commit

Permalink
fix: improved performance (only node@12 and above) (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Oct 23, 2020
1 parent a12730f commit 3c50404
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 12 additions & 0 deletions test/parallel-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -117,6 +124,7 @@ describe('parallel option', () => {

expect(Worker).toHaveBeenCalledTimes(1);
expect(Worker).toHaveBeenLastCalledWith(workerPath, {
enableWorkerThreads: ENABLE_WORKER_THREADS,
numWorkers: 2,
});
expect(workerTransform).toHaveBeenCalledTimes(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 3c50404

Please sign in to comment.