Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is LIBUV needed in Node JS?

So, maybe this question is too noob and novice to be asked but I still have no clue why LIBUV got a place in Node JS Architecture? So here is my understanding of NodeJs architecture.

  1. Node Js is built over V8
  2. V8 is capable of running code written with EcmaScript standards.
  3. V8 is written in C++.
  4. So if you want to give any new functionality we can embed V8 in our C++ project and attach new code with new Embedded V8 in C++.

Now here is the doubt,

  1. Since V8 supports EcmaScript Javascript that means it has the capability to run callbacks written with the standards of EcmaScript.
  2. So we can add code for File System Access, HTTP server & DB access in C++ since there are libraries (header files) that gives that functionality since Java is written in C++ (correct me if I am wrong) and Java has the capability to do the same.
  3. Now if we can add this functionality in C++ where does the place for Libuv come into the picture of NodeJs architecture.

Thanks in advance and Happy Coding :)

like image 628
Ankur Verma Avatar asked May 19 '19 07:05

Ankur Verma


People also ask

Why does node use libuv?

LibUV is the core engine that powers Nodejs. LibUV provides support for asynchronous I/O operations. It's a C based library primarily created for Nodejs and used by Luvit, Julia, pyuv, and some other software. LibUV enforces an asynchronous, event-driven style of programming.

What is libuv used for?

libuv is a multi-platform C library that provides support for asynchronous I/O based on event loops. It supports epoll(4) , kqueue(2) , Windows IOCP, and Solaris event ports. It is primarily designed for use in Node. js but it is also used by other software projects.

What is V8 what is libuv and what is the role of these two in node?

V8 engine is used to execute the javascript code we write and libuv is a lbrary used to provide multi threading feature in Nodejs to execute long running processes. Event loop is single threaded but Nodejs is not single threaded as it has a libuv threadpool in its runtime which is responsible for multi threading.

How does libuv work under the hood?

Libuv is an open-source library that handles the thread-pool, doing signaling, inter process communications all other magic needed to make the asynchronous tasks work at all. Libuv was originally developed for Node. js itself as an abstraction around libev , however, by now, multiple projects are already using it.

What is libuv in Node JS?

To prevent this, libuv is used in Node.js which facilitates a non-blocking I/O. It also has mechanisms to handle services like File System, DNS, network, child processes, pipes, signal handling, polling, and streaming.

What is libuv in V8?

In simple words, libuv is a library that allows your JavaScript code (via V8) to perform I/O, whether it is network, file etc. So from TCP level connectivity all the way to file/system ops are actually performed by the libuv library.

What is a thread pool in libuv?

To perform blocking operations that can’t be done asynchronously at OS level, libuv also includes a thread pool to distribute CPU loads. What is a thread pool? Libuv assigns tasks to a pool of worker threads. However, all callbacks that occur on task completion are executed on the main thread.

What are the different mechanisms used in libuv?

It uses platform-specific mechanisms as mentioned earlier to achieve the best compatibility and performance epoll (Linux), kqueue (OSX), IOCP (Windows), event ports (SunOS). File I/O: File I/O is implemented in libuv using a global thread pool on which all loops can queue work.


1 Answers

Check the docs below -

https://nodejs.org/en/docs/meta/topics/dependencies/#libuv

Another important dependency is libuv, a C library that is used to abstract non-blocking I/O operations to a consistent interface across all supported platforms. It provides mechanisms to handle file system, DNS, network, child processes, pipes, signal handling, polling and streaming. It also includes a thread pool for offloading work for some things that can't be done asynchronously at the operating system level.

So to sum it up, V8 provides the functionalities related to running JS files, but to use system resources like Network, Files, etc., libuv is used. Also it provides a threading model for accessing the resources mentioned.

like image 128
Shobhit Chittora Avatar answered Oct 05 '22 01:10

Shobhit Chittora