Stream writable.cork() and uncork() Method in Node.js

The writable.cork() method is used for forcing all written data to be buffered in memory. This buffered data will only be flushed when stream.uncork() or stream.end() methods are called. These methods are useful for optimizing performance by batching multiple write operations.

Syntax

cork()

writable.cork()

uncork()

writable.uncork()

Parameters

Both methods take no parameters. The cork() method buffers subsequent write operations, while uncork() flushes the buffered data to the underlying destination.

How It Works

When cork() is called, all subsequent write operations are buffered in memory instead of being written immediately. This continues until uncork() is called, which flushes all buffered writes at once. This batching can improve performance for multiple small writes.

Example: Using cork() to Buffer Data

// 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
writable.write('Hi - This data is printed');

// 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');
Hi - This data is printed

Only the data written before cork() is displayed immediately. The remaining data is buffered in memory until uncork() is called.

Example: Using uncork() to Flush Buffered Data

// Program to demonstrate writable.uncork() 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
writable.write('Hi - This data is printed');

// 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');

// Flushing the data from buffered memory
writable.uncork();
Hi - This data is printed
Welcome to TutorialsPoint !
SIMPLY LEARNING 
This data will be corked in the memory

All buffered data is flushed and displayed once uncork() is called.

Key Points

  • cork() buffers subsequent write operations in memory
  • uncork() flushes all buffered writes at once
  • Useful for optimizing performance with multiple small writes
  • stream.end() also flushes buffered data automatically

Conclusion

The cork() and uncork() methods provide efficient batching of write operations in Node.js streams. Use them to optimize performance when performing multiple small writes by buffering them and flushing all at once.

Updated on: 2026-03-15T23:19:00+05:30

760 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements