Published on
View count
4 views

Node.js File Writing Methods - createWriteStream vs writeFileSync

Authors

When working with Node.js file system operations, choosing the right method to write files can significantly impact your application's performance. Let's explore two common approaches: fs.createWriteStream() and fs.writeFileSync().

Key Differences at a Glance

Featurefs.createWriteStream(path).write(buffer)fs.writeFileSync(path, buffer)
SynchronicityAsynchronousSynchronous
BlockingNon-blockingBlocking
PerformanceBetter for large files or frequent writesBetter for small, infrequent writes
Memory UsageMore memory-efficient for large filesLoads entire content into memory
Error HandlingRequires callback or event listenersThrows errors directly
Use CaseStreaming large files, real-time dataQuick, simple writes, small files
Control FlowContinues execution immediatelyWaits until write is complete
Backpressure HandlingHandles backpressure automaticallyN/A

Using createWriteStream

The stream-based approach is ideal for handling large files or when you need to write data frequently:

const fs = require('fs')
 
// Content to write
const content = 'Hello, Node.js file system!'
 
// Create a write stream
const writeStream = fs.createWriteStream('example1.txt')
 
// Write to the file
writeStream.write(content, 'UTF8')
 
// End the stream
writeStream.end()
 
// Handle finish event
writeStream.on('finish', () => {
  console.log('Write completed.')
})
 
// Handle error event
writeStream.on('error', (err) => {
  console.error('An error occurred:', err.message)
})
 
console.log('Program continues executing immediately.')

Key Benefits 🎯

  • Non-blocking operation
  • Memory efficient for large files
  • Automatic backpressure handling
  • Ideal for real-time data processing

Using writeFileSync

For simple, one-off writes with small files, the synchronous approach might be more straightforward:

const fs = require('fs')
 
// Content to write
const content = 'Hello, Node.js file system!'
 
try {
  // Write to the file synchronously
  fs.writeFileSync('example2.txt', content, 'UTF8')
  console.log('Write completed.')
} catch (err) {
  console.error('An error occurred:', err.message)
}
 
console.log('This line waits for the write operation to complete.')

Key Benefits 🎯

  • Simple error handling with try-catch
  • Predictable execution flow
  • Perfect for configuration files or small data writes
  • No callback management needed

When to Use Each Method

Choose createWriteStream when:

  • Working with large files
  • Handling real-time data streams
  • Memory efficiency is crucial
  • Non-blocking operation is required

Choose writeFileSync when:

  • Writing small configuration files
  • Simple, one-off writes
  • Synchronous operation is acceptable
  • Working with startup configurations

Conclusion

Both methods have their place in Node.js development. createWriteStream shines in scenarios involving large files or frequent writes, while writeFileSync is perfect for simple, small file operations. Choose based on your specific use case, considering factors like file size, write frequency, and performance requirements.