Skip to Content
APIwriteFile

writeFile

Write data to a file, replacing the file if it exists. Supports encodings, mode, and flag.

Basic usage

import { writeFile } from '@rush-fs/core' await writeFile('./out.txt', 'hello world') await writeFile('./out.bin', buffer) await writeFile('./out.txt', 'content', { encoding: 'utf8', mode: 0o644 })

Methods

writeFile(path, data, options?)

Async. Returns Promise<void>.

ArgumentTypeDescription
pathstringFile path.
datastring | BufferData to write.
optionsobjectOptional. See below.

Options: encoding (utf8, ascii, latin1, base64, base64url, hex), mode (number), flag (e.g. 'w', 'wx', 'a', 'ax').

writeFileSync(path, data, options?)

Sync. Same arguments; throws on error.

Performance

From repo benchmarks (pnpm build && pnpm bench):

ScenarioNode.jsRush-FSRatio
writeFile small (string)74 µs66 µs0.9x
writeFile small (Buffer)115 µs103 µs0.9x
writeFile 4 MB string2.93 ms5.69 msNode.js faster (large string over N-API)

Single-file writes are on par with Node.js for small payloads; for very large strings Node.js can be faster due to N-API copy cost.

Notes

  • Encodings: Same as Node.js (utf8, ascii, latin1, base64, base64url, hex).
  • Large data: Writing a very large string (e.g. 4 MB) crosses the N-API boundary and can be slower than Node.js; prefer Buffer for large binary data where possible.
Last updated on