-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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 for
ftruncateSync
PR-URL: #50032 Refs: nodejs/performance#106 Reviewed-By: Yagiz Nizipli <[email protected]>
- Loading branch information
1 parent
ceedb3a
commit ed49722
Showing
3 changed files
with
46 additions
and
10 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,40 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const fs = require('fs'); | ||
const tmpdir = require('../../test/common/tmpdir'); | ||
tmpdir.refresh(); | ||
|
||
const path = tmpdir.resolve(`new-file-${process.pid}`); | ||
fs.appendFileSync(path, 'Some content.'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
type: ['invalid', 'valid'], | ||
n: [1e4], | ||
}); | ||
|
||
function main({ n, type }) { | ||
let fd; | ||
|
||
switch (type) { | ||
case 'invalid': | ||
fd = 1 << 30; | ||
break; | ||
case 'valid': | ||
fd = fs.openSync(path, 'r+'); | ||
break; | ||
default: | ||
throw new Error('Invalid type'); | ||
} | ||
|
||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
try { | ||
fs.ftruncateSync(fd, 4); | ||
} catch { | ||
// do nothing | ||
} | ||
} | ||
bench.end(n); | ||
if (type === 'valid') fs.closeSync(fd); | ||
} |
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