10

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?

user835611
  • 2,309
  • 2
  • 21
  • 29
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
  • 1
    Answers here are now outdated. See https://v8project.blogspot.com/2015/07/code-caching.html – EricLaw Feb 16 '17 at 17:40
  • @EricLaw 1. Feel free to add an answer and I'll mark it as accepted. 2. Hi! – mikemaccana Feb 17 '17 at 23:24
  • 1
    Possible duplicate of [How can I see the machine code generated by v8?](https://stackoverflow.com/questions/277423/how-can-i-see-the-machine-code-generated-by-v8) – mikemaccana May 21 '18 at 09:24

3 Answers3

11

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.

user835611
  • 2,309
  • 2
  • 21
  • 29
  • But you can only see the bytecode, though. :/ I'd like to run it too. –  Aug 27 '17 at 15:23
  • This is interesting, but doesn't actually answer the question of whether (or where) node.js **stores** the compiled code e.g. between runs. – natevw Jan 24 '20 at 20:18
10

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.

user568109
  • 47,225
  • 17
  • 99
  • 123
  • 2
    I disagree with "it does not make sense to store the machine code". It makes a lot of sense actually. JavaScript has grown beyond being used for short scripts to add features to web pages, and there are a lot of large scale programs that use it. The start up time for these applications is aggravating to sit through repetitively. If the bytecode were cached, the start up time would be greatly reduced. .NET, Python, Java cache machine (or bytecode) compiled code as an optimization to address this. I don't see why the V8 engine couldn't do the same. – Todd Feb 10 '20 at 18:29
  • This is not correct. Even if vm is JIT you can still compile ahead bytecode to speed up parsing part. – Enerccio Sep 30 '22 at 08:15
5

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.

Community
  • 1
  • 1
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • V8 being a just in time compiler will never generate static machine code for javascript like python compiler, so you cannot really see the machine code compiled. – user568109 May 21 '13 at 15:56
  • @denys-séguret But _is_ there a ~~bytecode~~, pardon, machine code cache? Meaning: it detects unchanged .js files and reuses already generated machine code? (In that sense being analogue to PHP's bytecode cache) – Frank N May 19 '16 at 07:39