diff --git a/benchmark/buffers/buffer-write-string-utf8.js b/benchmark/buffers/buffer-write-string-utf8.js new file mode 100644 index 000000000000..930b83dcd55b --- /dev/null +++ b/benchmark/buffers/buffer-write-string-utf8.js @@ -0,0 +1,36 @@ +'use strict'; + +// buf.write(string, 'utf8') for strings whose in-memory representation is +// one-byte (Latin-1) or two-byte (UTF-16), which take different encoder paths. +const common = require('../common.js'); +const bench = common.createBenchmark(main, { + chars: ['one-byte', 'two-byte', 'two-byte-astral', 'two-byte-lone-surrogate'], + len: [16, 256, 2048, 65536], + n: [5e5], +}); + +function makeString(chars, len) { + switch (chars) { + case 'one-byte': + return 'aé'.repeat(len / 2); + case 'two-byte': + return 'aé€日'.repeat(len / 4); + case 'two-byte-astral': + return 'aé€日\u{1F600}'.repeat(len / 6).padEnd(len, 'a'); + case 'two-byte-lone-surrogate': + return 'aé€日'.repeat(len / 4 - 1) + 'ab\ud800c'; + default: + throw new Error(chars); + } +} + +function main({ chars, len, n }) { + const string = makeString(chars, len); + const buf = Buffer.allocUnsafe(Buffer.byteLength(string)); + if (len >= 65536) n = Math.floor(n / 32); + bench.start(); + for (let i = 0; i < n; ++i) { + buf.write(string, 0, 'utf8'); + } + bench.end(n); +} diff --git a/doc/api/fs.md b/doc/api/fs.md index a880c54dc69e..2c3090945c4f 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -7407,6 +7407,13 @@ Additionally, when [`fs.readdir()`][] or [`fs.readdirSync()`][] is called with the `withFileTypes` option set to `true`, the resulting array is filled with {fs.Dirent} objects, rather than strings or {Buffer}s. +When a directory is read, such as with [`fs.readdir()`][] or +[`fs.opendir()`][], the file type of each entry is the type reported by the +operating system and may depend on the file system; for example, some file +systems may report a type that differs from what [`fs.lstat()`][] returns. +Node.js calls [`fs.lstat()`][] on such an entry only when the reported type +is unknown. Use [`fs.lstat()`][] when an accurate file type is required. + #### `dirent.isBlockDevice()`