0

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"?

ceving
  • 21,900
  • 13
  • 104
  • 178
Hayk
  • 187
  • 1
  • 10
  • `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" – Jaromanda X Jun 30 '21 at 02:43
  • The article was this - [link](https://codeburst.io/understanding-javascripts-engine-with-cartoons-3ef56487a987) – Hayk Jun 30 '21 at 02: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. – phuzi Jun 30 '21 at 10:20

1 Answers1

4

(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

jmrk
  • 34,271
  • 7
  • 59
  • 74
  • 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? – Hayk Jul 01 '21 at 15:34
  • Try v8.dev/blog. – jmrk Jul 01 '21 at 16:22