Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Node.js Buffer.kMaxLength Property

  • Last Updated : 08 Jun, 2021

The Buffer.kMaxLength property is an inbuilt application programming interface of class Buffer within buffer module which is used to set and get the maximum length allowed for single buffer instance.

Syntax:

const Buffer.kMaxLength

Parameters: This property works as a getter and setter so it takes integer value as a parameter sometimes.

Return Value: This property return the maximum length allowed for single buffer instance.

Example 1: Filename: index.js



Javascript




// Node.js program to demonstrate the
// Buffer.kMaxLength property
 
// Creating and initializing arraybuffer object
const arrbuff = new ArrayBuffer(16);
 
// Getting buffer object form existing
// arraybuffer object
const buffer = Buffer.from(arrbuff);
 
// Setting the the maximum value
// into the buffer
buffer.kMaxLength = 23;
 
// Getting the maximum length by using
// kMaxLength property
const value = buffer.kMaxLength;
 
// Display the result
console.log("kMaxLength is: " + value);

Output:

kMaxLength is: 23

Example 2: Filename: index.js

If kMaxLength is not initialized

Javascript




// Node.js program to demonstrate the
// Buffer.kMaxLength property
 
// If kMaxLengthis is not initialized
 
// Creating and initializing arraybuffer
// object
const arrbuff = new ArrayBuffer(16);
 
// Getting buffer object form existing
// arraybuffer object
const buffer = Buffer.from(arrbuff);
 
// Getting the maximum length by using
// kMaxLength property
const value = buffer.kMaxLength;
 
// Display the result
console.log("kMaxLength is: " + value);

Output:

kMaxLength is: undefined

Run index.js file using the following command:

node index.js

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/buffer.html#buffer_buffer_kmaxlength




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!