Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Stream writable.writableLength Property in Node.js
The writable.writableLength property returns the number of bytes (or objects) in the queue waiting to be written. This property is useful for monitoring buffer status and understanding the backpressure in your writable streams.
Syntax
writable.writableLength
How It Works
The property counts data that is:
- Buffered in the internal queue
- Waiting to be processed by the
_write()method - Corked (temporarily held) in memory
Data that has already been written or is currently being processed is not counted.
Example 1: Basic Usage with Cork
Create a file named writableLength.js and run it with node writableLength.js:
// Program to demonstrate writable.writableLength property
const stream = require('stream');
// Creating a writable stream
const writable = new stream.Writable({
// Writing the data from stream
write: function(chunk, encoding, next) {
// Converting the data chunk to be displayed
console.log(chunk.toString());
next();
}
});
// Writing data - Not in the buffer queue (written immediately)
writable.write('Hi - This data will not be counted');
// Calling the cork() function to buffer subsequent writes
writable.cork();
// Writing data while corked - goes to buffer queue
writable.write('Welcome to TutorialsPoint !');
writable.write('SIMPLY LEARNING ');
writable.write('This data will be corked in the memory');
// Printing the length of the queued data
console.log('Buffered bytes:', writable.writableLength);
Hi - This data will not be counted Buffered bytes: 81
Example 2: Cork and Uncork Demonstration
// Program to demonstrate cork/uncork with writableLength
const stream = require('stream');
// Creating a writable stream
const writable = new stream.Writable({
write: function(chunk, encoding, next) {
console.log(chunk.toString());
next();
}
});
// Writing data immediately
writable.write('Hi - This data will not be counted');
// Cork the stream to buffer writes
writable.cork();
// Add data to buffer queue
writable.write('Welcome to TutorialsPoint !');
writable.write('SIMPLY LEARNING ');
writable.write('This data will be corked in the memory');
console.log('Before uncork:', writable.writableLength);
// Flush buffered data
writable.uncork();
console.log('After uncork:', writable.writableLength);
Hi - This data will not be counted Before uncork: 81 Welcome to TutorialsPoint ! SIMPLY LEARNING This data will be corked in the memory After uncork: 0
Key Points
-
Immediate writes: Data written when the stream is not corked gets processed immediately and doesn't contribute to
writableLength -
Buffered writes: Data written while the stream is corked gets queued and counted in
writableLength -
After uncork: Once
uncork()is called, buffered data is flushed andwritableLengthreturns to 0 - Monitoring: This property helps monitor backpressure and buffer usage in streams
Common Use Cases
- Monitoring stream buffer usage
- Implementing flow control in custom streams
- Debugging stream performance issues
- Managing memory usage in high-throughput applications
Conclusion
The writableLength property provides insight into your stream's internal buffer state. Use it to monitor buffered data and implement proper backpressure handling in your Node.js applications.
Advertisements
