rsnext/bench/recursive-copy/run.js
Rinku Chaudhari dff39acc30
fix typo in comment and unused variable remove (#45307)
<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2023-02-02 14:55:23 -08:00

59 lines
1.3 KiB
JavaScript

import { join } from 'path'
import { ensureDir, outputFile, remove } from 'fs-extra'
import recursiveCopyNpm from 'recursive-copy'
import { recursiveCopy as recursiveCopyCustom } from 'next/dist/lib/recursive-copy'
const fixturesDir = join(__dirname, 'fixtures')
const srcDir = join(fixturesDir, 'src')
const destDir = join(fixturesDir, 'dest')
const createSrcFolder = async () => {
await ensureDir(srcDir)
const files = new Array(100)
.fill(undefined)
.map((_, i) =>
join(srcDir, `folder${i % 5}`, `folder${i + (1 % 5)}`, `file${i}`)
)
await Promise.all(files.map((file) => outputFile(file, 'hello')))
}
async function run(fn) {
async function test() {
const start = process.hrtime()
await fn(srcDir, destDir)
const timer = process.hrtime(start)
const ms = (timer[0] * 1e9 + timer[1]) / 1e6
return ms
}
const ts = []
for (let i = 0; i < 10; i++) {
const t = await test()
await remove(destDir)
ts.push(t)
}
const sum = ts.reduce((a, b) => a + b)
const nb = ts.length
const avg = sum / nb
console.log({ sum, nb, avg })
}
async function main() {
await createSrcFolder()
console.log('test recursive-copy npm module')
await run(recursiveCopyNpm)
console.log('test recursive-copy custom implementation')
await run(recursiveCopyCustom)
await remove(fixturesDir)
}
main()