-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fs: improve mkdtemp performance for buffer prefix
PR-URL: #51078 Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
- Loading branch information
1 parent
979e183
commit 65df2c6
Showing
3 changed files
with
50 additions
and
17 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,43 @@ | ||
'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: ['valid-string', 'valid-buffer', 'invalid'], | ||
n: [1e4], | ||
}); | ||
|
||
function main({ n, type }) { | ||
tmpdir.refresh(); | ||
const options = { encoding: 'utf8' }; | ||
let prefix; | ||
let out = true; | ||
|
||
switch (type) { | ||
case 'valid-string': | ||
prefix = tmpdir.resolve(`${Date.now()}`); | ||
break; | ||
case 'valid-buffer': | ||
prefix = Buffer.from(tmpdir.resolve(`${Date.now()}`)); | ||
break; | ||
case 'invalid': | ||
prefix = tmpdir.resolve('non-existent', 'foo', 'bar'); | ||
break; | ||
default: | ||
new Error('Invalid type'); | ||
} | ||
|
||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
try { | ||
out = fs.mkdtempSync(prefix, options); | ||
} catch { | ||
// do nothing | ||
} | ||
} | ||
bench.end(n); | ||
assert.ok(out); | ||
} |
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