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?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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.

Solution 2

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.

Solution 3

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.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply