V8 JavaScript engine gets eager compilation hints, but will devs use sparingly as advised?

V8 JavaScript engine gets eager compilation hints, but will devs use sparingly as advised?

The V8 JavaScript engine, used by the Chrome web browser, Node.js and elsewhere, has a new feature which lets developers mark a file for early compilation, with strong benefits for load time provided the option is used sparingly.

Explicit JavaScript compile hints, available in Chrome 136 which was released earlier this week, enables developers to mark a JavaScript file with the “magic comment”:

//# allFunctionsCalledOnLoad

at the top of the file, which instructs V8 to parse and compile the code in that file immediately on load, called eager compilation. Files which do not have this comment may have compilation deferred until a function is called. The design of V8 means that eager compilation happens on a background thread, whereas deferred compilation happens on the main thread, impacting performance.

According to Google software engineer Marja Hölttä, experiments with popular web pages showed performance improvements in 17 out of 20, with an average 630ms reduction in foreground parse and compile times – a significant improvement. The initial load time for a web application is critical to perceived responsiveness.

Hölttä explained how to log function events to test the outcome in specific cases. Currently the feature works only for complete files, but she said that there is a long-term plan to enable selecting individual functions. In the meantime, she suggests creating a core file with critical code and marking that for eager compilation.

The feature has been in progress for over two years. A paper from April 2023 explained that explicit compile hints override the PIFE (possibly invoked function expressions) heuristic that is otherwise used by V8 and which eagerly compiles all parenthesized functions. Downsides of PIFE are that it forces use of function expressions and that it cannot be applied to ECMAScript 6 class methods.

Another big issue, with both PIFE and the new feature, is overuse. Regarding explicit JavaScript compile hints, Hölttä said that “this feature should be used sparingly – compiling too much will consume time and memory.” The risk is that developers or optimizers treat the hints as something to apply to all their code, which might then have the opposite effect, slowing load times. Deferred or lazy parsing also has benefits; it ” speeds up startup and reduces memory overhead of applications that ship more code than they need,” said the earlier post from the V8 team. 

Developers have reacted accordingly. “On a fast internet connection many heavier websites’ load time has js parse/compile time as a large component, being able to parallelize that to any extent is great,” said one; but another said that “the hints will be abused, and eventually disabled altogether.”

Noting this problem, Google’s initial public paper on the subject said that “we might try to detect at run time that a site overuses compile hints, crowdsource the information, and use it for scaling down compilation for such sites,” but there is no suggestion of such a tactic being used in this initial rollout.