Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does nodejs/V8 store compiled machine code anywhere on disk?

Edit: Node uses bytecode since Node 8.3, before that, sources were compiled directly to machine code.

I do a lot of Python coding, and there's always bytecode lying around in .pyc files.

I was wondering if node stores its machine code in similar files, eg it would make sense to keep the machine code representation around on disk and re-use it if a file's source is unchanged.

If so, where does node/v8 store this machine code?

Edit 2: As @dystroy mentions below this is a dupe of How can I see the machine code generated by v8?

like image 600
mikemaccana Avatar asked May 21 '13 15:05

mikemaccana


People also ask

Does node compile to machine code?

Both your browser JavaScript and Node. js run on the V8 JavaScript runtime engine. This engine takes your JavaScript code and converts it into a faster machine code. Machine code is low-level code which the computer can run without needing to first interpret it.

How V8 engine works in node?

The Ignition interpreter compiles JavaScript code and generates non-optimized machine code when a developer or program runs it on V8 (i.e. in a browser or Node environment). The Turbofan and Crankshaft components of V8 examine and recompile the machine code at runtime for optimal performance.

What is V8 and what purpose does it serve on the NodeJS platform?

The V8 JavaScript engine is the core technology that translates your JavaScript source code into machine instructions. V8 is an open source JavaScript and WebAssembly engine, used in the Google Chrome web browser and in Node. js.

What is V8 engine in NodeJS?

What is V8? V8 is Google's open source high-performance JavaScript and WebAssembly engine, written in C++. It is used in Chrome and in Node.js, among others. It implements ECMAScript and WebAssembly, and runs on Windows 7 or later, macOS 10.12+, and Linux systems that use x64, IA-32, ARM, or MIPS processors.


3 Answers

V8 introduced a bytecode interpreter, Ignition, in 2016. You can print the bytecode with --print-bytecode (Node 8.3 and newer).

$ node --print-bytecode incrementX.js -e 'function incrementX(obj) {return 1 + obj.x;} incrementX({x: 42});`
...
[generating bytecode for function: incrementX]
Parameter count 2
Frame size 8
  12 E> 0x2ddf8802cf6e @    StackCheck
  19 S> 0x2ddf8802cf6f @    LdaSmi [1]
        0x2ddf8802cf71 @    Star r0
  34 E> 0x2ddf8802cf73 @    LdaNamedProperty a0, [0], [4]
  28 E> 0x2ddf8802cf77 @    Add r0, [6]
  36 S> 0x2ddf8802cf7a @    Return
Constant pool (size = 1)
0x2ddf8802cf21: [FixedArray] in OldSpace
 - map = 0x2ddfb2d02309 <Map(HOLEY_ELEMENTS)>
 - length: 1
           0: 0x2ddf8db91611 <String[1]: x>
Handler Table (size = 16)

See Understanding V8's Bytecode.

To see the machine code, use --print-opt-code --code-comments.

like image 56
user835611 Avatar answered Oct 20 '22 14:10

user835611


V8 is a just in time compiler. So JavaScript cannot be compiled just once like python compiler which is static compilation. It is compiled as and when it needs to be executed.

You cannot see the generated machine code for JavaScript, because it is not stored. It does not make sense to store the machine code that was compiled, as compilation happens repeatedly and is affected by runtime optimisations. You don't get fixed machine code like for python, every time it happens.

like image 33
user568109 Avatar answered Oct 20 '22 16:10

user568109


From the project's page :

V8 compiles JavaScript source code directly into machine code when it is first executed. There are no intermediate byte codes, no interpreter.

That's why you won't find the bytecode, there is none.

Regarding the new question following your edit, I think this related question answers it mostly. Of course there's no reason in general for V8 to write the machine code on disk with the default setup. As this code changes a lot (see the link above, explaining how dynamic classes are created), that would be a gigantic overhead.

like image 39
Denys Séguret Avatar answered Oct 20 '22 15:10

Denys Séguret