Learn More About the Use of Nodejs Buffer

Learn More About the Use of Nodejs Buffer

JavaScript was originally designed for browsers, without a mechanism to read or manipulate binary data streams. The introduction of the Buffer class gives NodeJS the ability to manipulate file streams or network binary streams.

JavaScript was originally designed for browsers, without a mechanism to read or manipulate binary data streams. The introduction of the Buffer class gives NodeJS the ability to manipulate file streams or network binary streams.

Buffer basic concepts

The memory allocation of the Buffer object is not in the V8 heap memory, but Node applies for memory at the C++ level. It can be understood that a part of the space is opened in memory, but the memory allocation is completed by the Node level when used, and the release Automatically controlled by the gc mechanism of v8 in Node. The basic operation of Buffer is not repeated here, the official documentation is very detailed.

Buffer performance comparison

Generally, during network transmission, data needs to be converted into a buffer. Let's do a performance comparison experiment.

1. Use plain string to return to the client

const http = require('http');

let hello = ''
for (var i = 0; i < 10240; i++) {
  hello += "a";
}

console.log(`Hello:${hello.length}`)
// hello = Buffer.from(hello);

http.createServer((req, res) => {
  res.writeHead(200);
  res.end(hello);
}).listen(8001);

Use ab -c 200 -t 100 http://127.0.0.1:8001/ command for performance testing, and initiate 200 concurrent clients

This is image title

Using strings, QPS can reach 4019.70 and the transmission rate is 40491.45KB per second.

2. Use Buffer. Converts the string into a Buffer object and sends it to the client.

const http = require('http');

let hello = ''
for (var i = 0; i < 10240; i++) {
  hello += "a";
}

console.log(`Hello:${hello.length}`)
hello = Buffer.from(hello);

http.createServer((req, res) => {
  res.writeHead(200);
  res.end(hello);
}).listen(8001);

Cancel the comment of Buffer conversion, also use ab -c 200 -t 100 http://127.0.0.1:8001/test, and also launch 200 concurrent clients

This is image title

Using Buffer, the QPS reaches 7130.05 and the transmission rate is 71822.74KB per second. The performance is 177% of the original, which greatly saves server resources. The above comparison example is referenced in "Exploding Node JS".

So the question comes, why is there such a big performance improvement?

The reason is actually very simple. In the HTTP transmission of NodeJS, if the returned type is string, string type parameters will be converted into Buffer, and returned to the client by Node Stream. If we return the Buffer type directly, there is no conversion operation, and it returns directly, reducing the CPU reuse rate. This part of the logic is in the Node source code https://github.com/nodejs/node/blob/v10.9.0/lib/_http_outgoing.js#L612

In the performance comparison example above, string when returning , each request needs to string be replaced with a Buffer to return; when returning a Buffer directly, this Buffer is stored in memory when we start the service, and each request returns directly to memory Buffer can be used, so the QPS before and after the use of the buffer is improved a lot.

Therefore, when we write business code, some resources can be converted into Buffer types (such as static resource files such as js, css) in advance, and the buffer is directly returned to the client. For example, in some file forwarding scenarios, the obtained content is stored as Buffer is forwarded directly to avoid extra conversion operations.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Hire Nodejs Developer

Looking to **hire NodeJS Developer**[https://www.dataeximit.com/node-js-development/](https://www.dataeximit.com/node-js-development/ "https://www.dataeximit.com/node-js-development/") to build a perfect back-end to your website or...

How to get started Internationalization in JavaScript with NodeJS

Tutorial showing how to use the Intl JS API in NodeJS (i18n). We'll install a module to unlock the Intl API languages for Node and test out RelativeTimeFormat to translate and localise relative times in JavaScript.

How to Build a Vanilla JavaScript + NodeJS GUI Content Tool

Learn how to build a vanilla JavaScript + NodeJS GUI content tool that creates HTML files, with this beginner-friendly tutorial.