Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does installing bufferUtil and utf-8-validate drastically improve performance of the WS websocket library in Node.js?

While reading through the documentation for the WS websocket library, I noticed the following statement:

There are 2 optional modules that can be installed along side with the ws module. These modules are binary addons which improve certain operations. Prebuilt binaries are available for the most popular platforms so you don't necessarily need to have a C++ compiler installed on your machine.

npm install --save-optional bufferutil: Allows to efficiently perform operations such as masking and unmasking the data payload of the WebSocket frames.
npm install --save-optional utf-8-validate: Allows to efficiently check if a message contains valid UTF-8 as required by the spec.

Sure enough, installing these modules gave me drastically better performance. My question is: why do they make such a huge difference? What about them makes the library so much faster? Is it because they were written in C++?


1 Answers

In short: yes.

With nodejs, you there are 2 sorts of modules:

  • Regular NPM modules written in Javascript
  • C/C++ Addon modules

C/C++ Addon modules are compiled with a tool called node-gyp and distributed in files with the .node extension. Inside a node module, you can require a C/C++ Addon module exactly like you can require regular Javascript modules:

const addonModule = require('addonModule.node);
like image 196
Julien Klepatch Avatar answered Oct 28 '25 19:10

Julien Klepatch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!