Stream writable.writableLength Property in Node.js


The writable.writableLength property is used for displaying the number of bytes or objects which are there in the queue that are ready to be written. This is used for inspecting the data as per the status from highWaterMark.

Syntax

writeable.writableLength

Example 1

Create a file with name – writableLength.js and copy the below code snippet. After creating file, use the following command to run this code as shown in the example below −

node writableLength.js

 Live Demo

// Program to demonstrate writable.writableLength method
const stream = require('stream');

// Creating a data stream with writable
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
writable.write('Hi - This data will not be counted');

// Calling the cork() function
writable.cork();

// Again writing some data
writable.write('Welcome to TutorialsPoint !');
writable.write('SIMPLY LEARNING ');
writable.write('This data will be corked in the memory');
// Printing the length of the queue data
console.log(writable.writableLength);

Output

C:\home
ode>> node writableLength.js Hi - This data will not be counted 81

The data which is corked and inside the buffer queue is counted and printed in the console.

Example

Let's take a look at one more example.

 Live Demo

// Program to demonstrate writable.cork() method
const stream = require('stream');

// Creating a data stream with writable
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
writable.write('Hi - This data will not be counted');

// Calling the cork() function
writable.cork();

// Again writing some data
writable.write('Welcome to TutorialsPoint !');
writable.write('SIMPLY LEARNING ');
writable.write('This data will be corked in the memory');

// Printing the length of the queue data
console.log(writable.writableLength);

// Flushing the data from buffered memory
writable.uncork()

console.log(writable.writableLength);

Output

C:\home
ode>> node writableLength.js Hi - This data will not be counted 81 Welcome to TutorialsPoint ! SIMPLY LEARNING This data will be corked in the memory 0

Since the data now has been flushed after uncork(). The queue will not hold any data which is why the length returned is 0.

Updated on: 20-May-2021

133 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements