Nodejs: What does `process.binding` mean?

I have seen process.binding('...') many times, exploring the source code for node.js in github .

Can someone explain to me what this function does?

+73
c ++ javascript v8 undocumented-behavior
Jun 04 '14 at 16:33
source share
3 answers

This function returns an internal module, such as require. It is not publicly available, so you should not rely on it in your code, but you can use it to play with low-level node objects if you want to understand how everything works.

For example, timer_wrap registered here . This is an export Timer constructor. It is imported into lib/timers.js

+62
Jun 04 '14 at 16:39
source share

This is a function that essentially exits, captures a C ++ function and makes it available inside javascript. Take this example process.binding('zlib') which is used in zlib

Essentially, this happens when you get a zlib C ++ object and then it is used the rest of the time in javascript code.

So when you use zlib, you donโ€™t actually go out and grab the C ++ library, you use the Javascript library that wraps the C ++ function for you.

This makes it easier to use.

+11
Oct 24 '17 at 11:01
source share

process.binding connects the JavaScript side of Node.js to the C ++ side of Node.js. The C ++ side of node.js is where most of the internal work of everything that the node does is implemented. So most of your code ultimately relies on C ++ code. Node.js uses the power of C ++.

Here is an example:

 const crypto=require("crypto") const start=Date.now() crypto.pbkdf2("a", "b", 100000,512,sha512,()=>{ console.log("1":Date.now()-start) }) 

Crypto is a built-in module in Node.js for hashing and saving passwords. This is how we implement this in Node.js, but the actual hashing process happens on the C ++ side of the node.js.

process.binding("crypto") will send this process to the src exporters directory, where the C ++ world of Node.js. is located In this part, Node.js V8 will convert the node.js values โ€‹โ€‹that we put into our various programs, such as a boolean, or function, or object, and translate them into their C ++ equivalents.

After the Javascript code has been translated into C ++, libuv will happen and it will do all the heavy calculations to execute the above code on the C ++ side outside the loop of events in the thread pool.

0
Jun 10 '19 at 7:48
source share



All Articles