1

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?

3
  • A full parse does not involve generating bytecode. The source you quote explicitly specifies full parse/*emit*. Leaving out the emit step is what makes lazy parsing faster.
    – GOTO 0
    Jan 24, 2017 at 9:22
  • @GOTO0 So normally a full parse always also includes emitting bytecode/machine code, rather than just generating a syntax tree? Jan 24, 2017 at 10:22
  • I don't know the details, but a syntax check may well not involve generating any syntax tree at all. I'm not sure if generating an AST would be required in case a syntax error is encountered in order to print a meaningful error message.
    – GOTO 0
    Jan 24, 2017 at 10:33

1 Answer 1

5

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.

2
  • Do you have any resources that talk about this in more detail? Jan 24, 2017 at 12:14
  • The source is at chromium.googlesource.com/v8/v8/+/master/src/parsing/… (and other files in that directory). That's the most detail you can get. A document outlining current ongoing work is at docs.google.com/document/d/…. While there are a number of blog posts and the like about various V8 concepts on the web, I'm not aware of anything talking about the Preparser specifically.
    – jmrk
    Jan 24, 2017 at 14:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.