forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fs: improve error performance of
renameSync
PR-URL: nodejs#49962 Refs: nodejs/performance#106 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
- Loading branch information
1 parent
56091e2
commit 246d153
Showing
4 changed files
with
61 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const fs = require('fs'); | ||
const assert = require('assert'); | ||
const tmpdir = require('../../test/common/tmpdir'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
type: ['invalid', 'valid'], | ||
n: [2e3], | ||
}); | ||
|
||
function main({ n, type }) { | ||
switch (type) { | ||
case 'invalid': { | ||
let hasError = false; | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
try { | ||
fs.renameSync(tmpdir.resolve(`.non-existing-file-${i}`), tmpdir.resolve(`.new-file-${i}`)); | ||
} catch { | ||
hasError = true; | ||
} | ||
} | ||
bench.end(n); | ||
assert(hasError); | ||
break; | ||
} | ||
case 'valid': { | ||
tmpdir.refresh(); | ||
for (let i = 0; i < n; i++) { | ||
fs.writeFileSync(tmpdir.resolve(`.existing-file-${i}`), 'bench', 'utf8'); | ||
} | ||
|
||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
fs.renameSync( | ||
tmpdir.resolve(`.existing-file-${i}`), | ||
tmpdir.resolve(`.new-existing-file-${i}`), | ||
); | ||
} | ||
|
||
bench.end(n); | ||
break; | ||
} | ||
default: | ||
throw new Error('Invalid type'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters