Matt Zeunert
Matt Zeunert

Reputation: 16571

What makes pre-parsing a function faster than a full parse?

To improve performance JavaScript engines sometimes only fully parse functions when they are actually called.

For example, from the Spidermonkey source code:

Checking the syntax of a function is several times faster than doing a full parse/emit, and lazy parsing improves both performance and memory usage significantly when pages contain large amounts of code that never executes (which happens often).

What steps can the parser skip while still being able to validate the syntax?

It appears that in Spidermonkey some of the savings come from not emitting bytecode, like after a full parse. Does a full parse in e.g. V8 also include generating machine code?

Upvotes: 1

Views: 751

Answers (1)

jmrk
jmrk

Reputation: 40581

First off a clarification: the two steps are called "pre-parsing" and "full parsing". "Lazy parsing" describes the strategy of doing the former first, and then the latter when needed.

The two big reasons why pre-parsing is faster are:

  • it doesn't build the AST (abstract syntax tree), which is usually the output of the parser
  • it doesn't do scope resolution for variables.

There are a few other internal steps done by the parser in preparation for code generation that aren't needed when only checking for errors, but the above two are the major points.

(Full) parsing and code generation are separate steps.

Upvotes: 5

Related Questions