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.

nodejs javascript buffer

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

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

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

The essential JavaScript concepts that you should understand

The essential JavaScript concepts that you should understand - For successful developing and to pass a work interview

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 Install NodeJS on Ubuntu 19.04

In this tutorial, you will learn how to install Node onto Ubuntu 19.04 Disco Dingo. We will cover installation from the default repositories and, for those wanting more recent releases, how to install from the NodeSource repositories.

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.

Top 5 Reasons Javascript Developers Prefer Deno over Nodejs

The maker of NodeJS, Ryan Dahl, has released a new runtime that aims to solve many of the shortcomings of Node. Your initial reaction might be “Oh great, another Javascript framework? Just what I needed…”. Don’t worry, I had the same reaction. After understanding the advantages, I too saw why Deno is exactly what a back-end Javascript developer needs in 2020. Let’s take a look at the top 5 reasons javascript developers are having a much smoother and more modern experience using Deno vs. Node.