Skip to content

Parser

The parser converts JavaScript source code into an Abstract Syntax Tree (AST) — a tree representation of the code’s structure.

flowchart LR
A["Source: const x = 5;"] --> B["Lexer: Tokenizer"]
B --> C["Tokens: [const, x, =, 5, ;]"]
C --> D["Parser: Build AST"]
D --> E["AST: VariableDeclaration"]
// Source code
const x = 5;
// Tokens produced
// [Keyword: const] [Identifier: x] [Operator: =] [Number: 5] [Punctuator: ;]
Program
├── VariableDeclaration (const)
│ ├── VariableDeclarator
│ │ ├── Identifier (x)
│ │ └── NumericLiteral (5)

V8 uses two parsing modes:

  • Eager: Parses functions immediately (slower start)
  • Lazy: Skips function bodies until called (faster start, used by default)
// Lazily parsed — body skipped until called
function greet() {
console.log('Hello');
}
// Eagerly parsed with parens
const greet = (function() {
console.log('Hello');
});
  • Parser converts source → AST
  • Lexer tokenizes, Parser builds tree
  • V8 uses lazy parsing for faster startup
  • AST is input for the interpreter