Home > Back-end >  Do V8 execute Byte code produced by the interpreter or binary code produced by the turbofan compiler
Do V8 execute Byte code produced by the interpreter or binary code produced by the turbofan compiler

Time:09-17

I am confused about how v8 works and how it execute js code.

I understand that v8 first parse the code and create AST then take this AST introducing it to the interpreter as input and produce byte code then this byte code introduced to turbofan compiler that converts this intermediate code to machine code that computer understands.

I am reading an article about this topic which is good but it turned out that something is confusing to me

  1. Execution Phase: The byte code is executed by using the Memory heap and the Call Stack of the V8 engine’s runtime environment

I thought that machine code is the one that get executed otherwise why we need to create that code if the byte code is the one that is executed??

CodePudding user response:

(V8 developer here.)

To confirm what @Bergi said in his comments: bytecode primarily serves the purpose of being executed by the interpreter, and that is in fact all that's needed to execute JavaScript. V8 (nowadays) doesn't compile all functions to machine code, only those that run hot enough for optimized compilation to (likely) be worth the time investment. As a particular detail of the implementation, the optimizing compiler uses the bytecode as input, which gives bytecode a secondary purpose; but that's really just a detail that could have been solved differently, such as by parsing the original source to an AST again when the optimizing compiler kicks in, which is how V8 used to do it until a couple of years ago.

(The article you linked to does mention most of these concepts, but I agree that it's written somewhat confusingly, and I'd disagree with some of its characterizations. See v8.dev/blog for official descriptions of how things work.)

  • Related