JavaScript compile hints are a new V8 feature that lets you control when your code gets compiled. By adding a simple comment to your JavaScript files, you can force V8 to compile functions immediately during script loading instead of waiting until they’re called.
//# allFunctionsCalledOnLoad
function wazzzup1() { console.log("Wazzzup!");}
Note that the compile hint comment must be placed at the very top of the file. In Chrome 136, you can only mark entire files for eager compilation - individual function-level hints aren’t supported yet.
When V8 processes JavaScript, it has to decide whether to compile each function right away or defer compilation until the function is actually used. This decision impacts performance - if a function gets called during page load but wasn’t compiled eagerly, V8 has to stop and compile it on the main thread, blocking your app. The compile hints feature addresses this by letting you mark entire files for eager compilation.
Compile hints are useful for libraries or files that are critical to your app’s startup performance. By marking these files, you can ensure that all functions within them are compiled immediately, reducing the risk of blocking the main thread during page load. According to the V8 team’s testing:
Many web pages would benefit from selecting the correct functions for eager compilation. For example, in our experiment with popular web pages, 17 out of 20 showed improvements, and the average foreground parse and compile times reduction was 630 ms.
This 630 millisecond improvement can make a significant difference in how quickly your app becomes interactive.
You should avoid using compile hints on large utility libraries where only a few functions get called initially, or on code that loads conditionally. The key is identifying files where most functions actually run during page load. Compiling unnecessary code wastes memory and processing time.
Here’s a file without the compile hints so you can see the difference:
function wazzzup2() { console.log("Wazzzup!");}
You can verify compile hints are working by running Chrome with function logging:
➜ rm -rf /tmp/chromedata && google-chrome --no-first-run --user-data-dir=/tmp/chromedata --js-flags=--log-function_events > log.txt➜ grep wazzzup log.txtfunction,full-parse,5,47,83,0,78791,wazzzup1function,interpreter,5,47,83,0,78833,wazzzup1function,preparse-no-resolution,6,17,53,0,89416,wazzzup2function,full-parse,6,17,53,0,89458,wazzzup2function,parse-function,6,17,53,0,89458,wazzzup2function,interpreter,6,17,53,0,89500,wazzzup2
This will create a log file with all function events. If you see a parse-function
event for a function, it means V8 compiled it lazily when it was called. Functions compiled eagerly (with the compile hint) won’t show this event - they’re already compiled during initial script processing.
The feature works in Chrome 136 and later. Future versions plan to support marking individual functions for eager compilation, giving even more precise control over startup performance.
Compile hints solve JavaScript startup performance issues - with minimal effort. If you have a core file that always runs during page load, adding that single comment can significantly improve your initial load times.