1

I read article about how JavaScript Engine works, but there is thing that confusing me, it says the following:

  1. JavaScript code first parsed
  2. The source code translated to bytecode
  3. The bytecode gets optimized
  4. The code generator takes bytecode and translates into low level assembly code

Is last step true? Does above how JavaScript Engines work like "V8"?

3
  • Is last step true? no reason it can't be I guess - which article? Does it specifically mention any particular JS Engine(s) - thinking about it, I doubt the code is ever "assembly code" - since "assembly code" is just another language - perhaps the article meant "machine code" - as in actual processor instructions, rather than the abstraction "assembly code" Commented Jun 30, 2021 at 2:43
  • The article was this - link Commented Jun 30, 2021 at 2:47
  • FYI, the article talks about "JavaScript's engine". This seems to suggest there is a single JavaScript engine. There are many "engines", most notably: V8 (Chrome, Node, Deno), SpiderMonkey (FireFox), JavaScriptCore (Safari) and Chakra (IE and early version of Edge); which are all free to do this any way they see fit. I would imagine they are all fairly similar at a high level though. Commented Jun 30, 2021 at 10:20

1 Answer 1

5

(V8 developer here.)

Yes, the JavaScript engines used in "modern" (since 2008) browsers have just-in-time compilers that compile JavaScript to machine code. That's probably what that article meant to say. If you want to distinguish the terms "assembly language" and "machine code", then the former would be the human-readable form (such as mov eax, ebx, written by humans and produced by disassemblers) and the latter would be the binary-encoded form that the CPU understands (such as 0x89 0xD8, produced by compilers/assemblers). I'd say that the term "assembly code" is sufficiently ambiguous that it could refer to either, or could imply that you don't want to distinguish.

I find the third step in your description more misleading: byte code is typically not optimized. The bytecode interpreter, if it exists, is usually the engine's first execution tier, and its purpose is to start execution as soon as possible, without first spending any time on optimizations. If a function runs hot enough, the engine will eventually decide to spend the time to optimize it to machine code (depending on the engine, possibly in a succession of several increasingly powerful but costly compilers). These later, optimizing tiers may or may not take the bytecode as input; alternatively they can parse the source again to build an AST (taking V8 as a specific example, it used to do the latter and is currently doing the former).


Side note: that article is pretty silly indeed. Example:

techniques like inlining (removing white space)

That's so wrong that it's outright funny :-D

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your valuable answer, can you please give me some good resources where I can learn more about how truly V8 translates Javascript code into machine code and executes it?
Try v8.dev/blog.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.