Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does node.js compile JavaScript?

Node.js uses V8 and it compiles the JavaScript as an optimization strategy.

So, the JavaScript running at the server side via node.js / V8 is compiled or interpreted?

like image 429
Marcell Alves Avatar asked Mar 29 '17 20:03

Marcell Alves


2 Answers

V8 engine compiles javascript to a sequence of machine code instructions, one function at a time (usually, functions are not compiled until the first call).

V8 parses the code and extracts an AST (abstract syntax tree), performs scope analysis in order to understand to which context a symbol refers to, and translates it to machine code instructions.

As you mentioned, V8 is highly focused on performance: besides the full compiler that compiles each function, V8 consists of extra compiler which is responsible for optimizing blocks that identified as frequently used (Known as the Crankshaft)

So no, there's no interpretation of javascript code, but translation and execution of a machine code.

like image 141
Reuven Chacha Avatar answered Oct 19 '22 07:10

Reuven Chacha


Interpreter: A (core) module part of the language runtime / virtual machine which takes specific 'actions' against a set of expressions expressed in the language whose virtual machine the module is.

Compiler: A (core) module part of the language runtime which 'converts' a set of expressions expressed in the language whose compiler the module is, into a set of instructions native to the architecture where the expressions are run against.

Standard Node.js is built against V8, which compiles every Javascript code snippet into native instructions. You may use --print_code flag in the command line to see which scripts are getting compiled, and compiled into what.

Hope this helps.

like image 27
Gireesh Punathil Avatar answered Oct 19 '22 06:10

Gireesh Punathil