Today I Learned - Rocky Kev

TIL Libuv

POSTED ON:

TAGS:

Today I learned about Libuv!

Node is a JS environment so we can run JS server-side, and built on top of Google's V8 Javascript engine.

What about Livuv?

Libuv is a library that provides the underlying event loop and I/O functionalities used by Node.js. Node.js is built on top of libuv!

Libuv is also open-source!

Libuv also implements two extremely important features of Nodejs which are the event loop and the thread pool and in simple terms, the event loop is responsible for handling easy tasks like executing callbacks and network I/O while the thread pool deals with heavy work like file access or compression.

Written in

libuv is written in C++, Google's v8 engine is primarily written in C++.

Node.js is primarily written in C and C++, with some portions of the core written in JavaScript. The Node.js project uses C and C++ for its core functionality, such as the event loop and low-level I/O operations, while JavaScript is used for higher-level abstractions and user-facing APIs.

Performance and the Thread Pool

Some tasks are really heavy and expensive to be executed in the event loop because they will then block the single thread.

So that's where we use the Thread Pool.

The thread pool is used to execute operations that are CPU-bound or that require blocking I/O operations, such as file system access or network communication.

The use of a thread pool in combination with the event loop allows Node.js to handle a large number of concurrent operations efficiently, without blocking the event loop or requiring the use of multiple threads.

via Node.js Behind the Scenes


Related TILs

Tagged:

TIL what is npm Script

Despite their high usage they are not particularly well optimized and add about 400ms of overhead. In this article we were able to bring that down to ~22ms.

TIL fancy methods to transform Javascript Objects

You can use Object.entries(), Object.keys(), Object.fromEntries()...

TIL how to hide your JS code

ONE THING TO NOTE: Encrypting a script is stronger than obfuscation, both methods are still not adequate to protect secret content. Honestly, I don't think it's worth it on a production site, and instead just go with pure server-side if you want security. But it's fascinating.