Does node.js compile JavaScript?

断了今生、忘了曾经 提交于 2020-12-01 09:49:08

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/43103939/does-node-js-compile-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!