DEV Community

Divyesh Parmar
Divyesh Parmar

Posted on

Can we say that JS can be compiled on Node.js? [Novice Quesiton]

I am reading You Don't know JS it says,

but despite the fact that JavaScript falls under the general category of "dynamic" or "interpreted" languages, it is in fact a compiled language. It is not compiled well in advance, as are many traditionally-compiled languages, nor are the results of compilation portable among various distributed systems.

So is it trying to say that JS compiled on Node.js

my another question is if Node.js is created with C++ then why do you we call it a JS platform/framework? Is it like PyPy? where Python compiler itself runs Python?

Any explanation/advice/guidance is appreciated. Sorry if I've not used any more technical terminology.

Top comments (5)

Collapse
 
svemaraju profile image
Srikanth • Edited

Not exactly what you're asking, but checkout the blog post -- Is Python interpreted or compiled? Yes. by Ned Batchelder. It shows the answer to such a question can be a complicated one.

Collapse
 
rhymes profile image
rhymes

NodeJS contains v8 which is a JS engine written in C++ (the same that Chrome contains).

It handles compilation, garbage collection and optimizations.

Node is written in both C++ and JS. Python is written in C and Python.

Collapse
 
johnkennedy9147 profile image
John Kennedy

The difference between compiled and interpreted languages is blurred these days.

In the past you had compiled languages where the entire source code was compiled to platform dependant machine code then distributed versus interpreted languages where the source code was distributed and compilation was performed line by line as the program was executed.

Neither of these clearly describes what happens in many modern languages. Often compilation is carried out when the program is executed(Just in Time - JIT) or it maybe done in stages first to intermediate bytecode then distributed and at runtime compiled to machine code.

There are many other variations on when and how compilation takes place.

For JavaScript on browsers and Node its JIT compilation with each engine having variations on the specifics of how it handles the compilation.

Collapse
 
oathkeeper profile image
Divyesh Parmar

ohhh so for Python when it was run on the C++ compiler or it's implication it ran that way, but PyPy module changed it?

Is this like what android changed They had this Dalvik cache and they also implemented new one after Android 6.0 Marhsmallow or Android 7.0 Nougat.

Collapse
 
nepeckman profile image
nepeckman • Edited

What the author is trying to say is that JavaScript is a Just In Time compiled language (abbreviated JIT). JIT compiling is the process of compiling a line of code right before executing it. When Node is handed a file to execute, its still a JavaScript file. So there is no compilation step before Node runs. However, while Node is running, the JavaScript is compiled and immediately executed after the compile. Hence, Just In Time. This is true of both Node and Browser runtimes, both use JIT compilation to run JavaScript.

While this means JavaScript does undergo a compilation step, because the compiling occurs right before execution, I don't know if I'd say "JS can be compiled on node", even though that's technically true. Most people associate compiling with outputting a compiled file to be run later (Ahead of Time, or AOT), which does not happen for JavaScript.

Hope this helps, can provide more details if you need.