mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-09 21:07:46 +00:00
[build-utils] Rewrite rename() to be more efficient (#9322)
Around 6 months ago, @styfle brought to my attention how `rename()` in build-utils used `reduce()` and could be written better. So, I rewrote it. Before, the code would create a new `Files` object and copy the contents of the previous `Files` object. This caused heavy garbage collection and memory thrashing. Instead, I created a single `Files` object, then add the files to it. Results: | # Files | Before | After | |---|---|---| | 1,000 | 75 ms | 1 ms | | 10,000 | 10.6 s | 7 ms | | 20,000 | 44.6 s | 16 ms | | 30,000 | 105.5 s | 22 ms | | 100,000 | Too long | 73 ms |
This commit is contained in:
18
packages/build-utils/test/unit.test.ts
vendored
18
packages/build-utils/test/unit.test.ts
vendored
@@ -4,14 +4,17 @@ import fs from 'fs-extra';
|
||||
import { strict as assert } from 'assert';
|
||||
import { getSupportedNodeVersion } from '../src/fs/node-version';
|
||||
import {
|
||||
FileBlob,
|
||||
getNodeVersion,
|
||||
getLatestNodeVersion,
|
||||
getDiscontinuedNodeVersions,
|
||||
rename,
|
||||
runNpmInstall,
|
||||
runPackageJsonScript,
|
||||
scanParentDirs,
|
||||
Prerender,
|
||||
} from '../src';
|
||||
import type { Files } from '../src';
|
||||
|
||||
jest.setTimeout(10 * 1000);
|
||||
|
||||
@@ -451,3 +454,18 @@ it('should retry npm install when peer deps invalid and npm@8 on node@16', async
|
||||
'Warning: Retrying "Install Command" with `--legacy-peer-deps` which may accept a potentially broken dependency and slow install time.',
|
||||
]);
|
||||
});
|
||||
|
||||
describe('rename', () => {
|
||||
it('should rename keys of files map', () => {
|
||||
const before: Files = {};
|
||||
const toUpper = (s: string) => s.toUpperCase();
|
||||
|
||||
for (let i = 97; i <= 122; i++) {
|
||||
const key = String.fromCharCode(i);
|
||||
before[key] = new FileBlob({ contentType: 'text/plain', data: key });
|
||||
}
|
||||
|
||||
const after = rename(before, toUpper);
|
||||
expect(Object.keys(after)).toEqual('ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''));
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user