rsnext/test/e2e/app-dir/build-size/index.test.ts
Quentin abe8b1e0a8
Improve performance of String.prototype.split uses (#56746)
This PR adds the optional `limit` parameter on String.prototype.split uses.

> If provided, splits the string at each occurrence of the specified separator, but stops when limit entries have been placed in the array. Any leftover text is not included in the array at all.

[MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split#syntax)

While the performance gain may not be significant for small texts, it can be huge for large ones.

I made a benchmark on the following repository : https://github.com/Yovach/benchmark-nodejs

On my machine, I get the following results:
`node index.js`
> normal 1: 570.092ms
> normal 50: 2.284s
> normal 100: 3.543s

`node index-optimized.js`
> optmized 1: 644.301ms
> optmized 50: 929.39ms
> optmized 100: 1.020s

The "benchmarks" numbers are : 
- "lorem-1" file contains 1 paragraph of "lorem ipsum"
- "lorem-50" file contains 50 paragraphes of "lorem ipsum"
- "lorem-100" file contains 100 paragraphes of "lorem ipsum"
2023-10-19 00:25:15 +00:00

49 lines
1.6 KiB
TypeScript

import { createNextDescribe } from 'e2e-utils'
createNextDescribe(
'app-dir build size',
{
files: __dirname,
skipDeployment: true,
},
({ next, isNextStart }) => {
if (isNextStart) {
it('should have correct size in build output', async () => {
const regex = /(\S+)\s+([\d.]+\s\w+)\s+([\d.]+\s\w+)/g
const matches = [...next.cliOutput.matchAll(regex)]
const result = matches.reduce((acc, match) => {
const [, path, size, firstLoadJS] = match
acc[path] = { size, firstLoadJS }
return acc
}, {})
// convert pretty-bytes format into bytes so we can compare units
const sizeToBytes = (size: string) => {
const units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
const [value, unit] = size.split(' ', 2)
const exp = units.indexOf(unit)
return parseFloat(value) * Math.pow(1024, exp)
}
const index = result['/']
const foo = result['/foo']
// index route has a page, so it should not be 0
expect(sizeToBytes(index.size)).toBeGreaterThan(0)
expect(sizeToBytes(index.firstLoadJS)).toBeGreaterThan(0)
// foo route has a page, so it should not be 0
expect(sizeToBytes(foo.size)).toBeGreaterThan(0)
expect(sizeToBytes(foo.firstLoadJS)).toBeGreaterThan(0)
// foo is a client component, so it should be larger than index
expect(sizeToBytes(foo.size)).toBeGreaterThan(sizeToBytes(index.size))
})
} else {
it('should skip next dev for now', () => {})
return
}
}
)