-
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: add c++ fast path for writeFileSync utf8
PR-URL: #49884 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Santiago Gimeno <[email protected]> Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: James M Snell <[email protected]>
- Loading branch information
1 parent
483200f
commit e59268a
Showing
5 changed files
with
156 additions
and
0 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.js'); | ||
const fs = require('fs'); | ||
const tmpdir = require('../../test/common/tmpdir'); | ||
tmpdir.refresh(); | ||
|
||
// Some variants are commented out as they do not show a change and just slow | ||
const bench = common.createBenchmark(main, { | ||
encoding: ['utf8'], | ||
useFd: ['true', 'false'], | ||
length: [1024, 102400, 1024 * 1024], | ||
|
||
// useBuffer: ['true', 'false'], | ||
useBuffer: ['false'], | ||
|
||
// func: ['appendFile', 'writeFile'], | ||
func: ['writeFile'], | ||
|
||
n: [1e3], | ||
}); | ||
|
||
function main({ n, func, encoding, length, useFd, useBuffer }) { | ||
tmpdir.refresh(); | ||
const enc = encoding === 'undefined' ? undefined : encoding; | ||
const path = tmpdir.resolve(`.writefilesync-file-${Date.now()}`); | ||
|
||
useFd = useFd === 'true'; | ||
const file = useFd ? fs.openSync(path, 'w') : path; | ||
|
||
let data = 'a'.repeat(length); | ||
if (useBuffer === 'true') data = Buffer.from(data, encoding); | ||
|
||
const fn = fs[func + 'Sync']; | ||
|
||
bench.start(); | ||
for (let i = 0; i < n; ++i) { | ||
fn(file, data, enc); | ||
} | ||
bench.end(n); | ||
|
||
if (useFd) fs.closeSync(file); | ||
} |
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
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