Skip to content

Commit 966a23f

Browse files
committed
fixup! fs: add c++ fast path for writeFileSync utf8
1 parent 297d3db commit 966a23f

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

benchmark/fs/bench-writeFileSync.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const bench = common.createBenchmark(main, {
1515

1616
function main({ n, encoding, stringLength, hasFileDescriptor }) {
1717
const enc = encoding === 'undefined' ? undefined : encoding;
18-
const path = tmpdir.resolve(`.writefilesync-file-${process.pid}`);
18+
const path = tmpdir.resolve(`.writefilesync-file-${Date.now()}`);
1919

2020
const useFd = hasFileDescriptor === 'true';
2121
const file = useFd ? fs.openSync(path, 'w') : path;
@@ -24,14 +24,9 @@ function main({ n, encoding, stringLength, hasFileDescriptor }) {
2424

2525
bench.start();
2626
for (let i = 0; i < n; ++i) {
27-
try {
28-
fs.writeFileSync(file, data, enc);
29-
} catch {
30-
// do nothing
31-
}
27+
fs.writeFileSync(file, data, enc);
3228
}
3329
bench.end(n);
3430

35-
fs.unlinkSync(path);
3631
if (useFd) fs.closeSync(file);
3732
}

src/node_file.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2533,8 +2533,12 @@ static void WriteFileUtf8(const FunctionCallbackInfo<Value>& args) {
25332533
uv_buf_t uvbuf = uv_buf_init(*value, value.length());
25342534

25352535
FS_SYNC_TRACE_BEGIN(write);
2536-
uv_fs_write(nullptr, &req, file, &uvbuf, 1, 0, nullptr);
2536+
int err = uv_fs_write(nullptr, &req, file, &uvbuf, 1, 0, nullptr);
25372537
FS_SYNC_TRACE_END(write);
2538+
2539+
if (err < 0) {
2540+
return env->ThrowUVException(err, "write");
2541+
}
25382542
}
25392543

25402544
/*

0 commit comments

Comments
 (0)