Home > Back-end >  Function wont eager compile
Function wont eager compile

Time:02-16

I recently tried to eager compile my javascript function from the guide shown in the link-https://v8.dev/blog/code-caching-for-devs

But even after enclosing the function in IIFE heuristics as said above, the V8 System analyzer shows its lazy compiled, and unoptimized. It is not showing under the eager compile section. The creation time however shifted to the front, but there is no noticeable performance change. Does this mean that eager compile functions don't perform faster? Then what exactly is the advantage of a eager compile function?

(pls note, the below function is created for learning purposes. I use this to learn about eager compiling js functions): script.js:

const a = 1000;
const b = 2000;

const add = function(a,b) {
    var sampleObject2={
        firstName: "John",
        lastName: "Doe",
        age: 70,
        eyeColor: "blue"
      };
    var c=a**b;
    var d=b**c;
    c=c 21;
    d=d c;
    d=d**sampleObject2.age;
    console.log(c**d);
    return c**d;
};
window.addEventListener("click", ()=>{
  var start=performance.now()
  add(a,b);
  var end=performance.now()
  console.log(end-start);
},false);

CodePudding user response:

(V8 developer here.)

the V8 System analyzer shows its lazy compiled

That's not what I'm seeing: with the code as written in your question, i.e. const add = function(a, b) { ... };, the System Analyzer says type: LazyCompile~.
When I add the parentheses that (currently!) get interpreted as an eager-compilation hint, i.e. const add = (function(a, b) = { ... });, that changes to type: Function~ (as in your screenshot; so I guess you did actually add the parentheses before running the test that produced that screenshot).

and unoptimized

That is expected. Optimization has nothing to do with lazy compilation. There is no way to optimize a function right away, because attempting that would make no sense: the optimizer can't do anything useful if it doesn't have type feedback to work with, which can only be collected during unoptimized execution.

Does this mean that eager compile functions don't perform faster?

Correct, the function's own performance doesn't change at all.

Then what exactly is the advantage of a eager compile function?

Preparing a function for later lazy compilation has a small cost. In case the function will be needed soon, compiling it right away avoids this cost. In fact, I do see a tiny difference in the times that your test reports; although I haven't verified that this difference is actually due to eager vs. lazy compilation, it may well be caused by something else.

If eager-compiling everything was a good idea, then engines would do it -- that'd be much easier. In reality, lazy-compiling is almost always worth the tiny amount of extra work it creates, because it makes startup/pageload faster, and can avoid quite a bit of memory consumption when there are unused functions, e.g. in your included libraries. So JavaScript engines accept a lot of internal complexity to make lazy compilation possible. I wouldn't recommend attempting to side-step that.

(For completeness: a couple of years ago V8's implementation of lazy compilation was not as optimized as it is today, and manually hinting eager compilation could achieve more significant improvements, especially for deeply nested functions. But these times are gone! And now it's just one more example that illustrates that in general you shouldn't go to great lengths to adapt your code to what engines are currently doing well or not-so-well.)

  • Related