Search code examples
node.jsv8

Node.js %OptimizeFunctionOnNextCall - Runtime Function Error


I am running Node v11.x.x, and receiving an error for a V8 runtime function.

let operand = 3;
function square() {
    return operand * operand;
}
square()
%OptimizeFunctionOnNextCall(square);
square()

Running with:

node --allow-natives-syntax -trace_opt -trace_deopt main.js

Recieving an error of

ReferenceError: OptimizeFunctionOnNextCall is not defined

I thought this runtime function was still included, or am I doing something wrong.


Solution

  • It's because you're relying on automatic semicolon insertion by Javascript (which fails your intention).

    Your code without ; translates to modulo operation which is a valid JavaScript operation so it has no reason to add a ; for you after the first call square().

    square() % OptimizeFunctionOnNextCall(square);
    

    Written this way (as JavaScript sees), it obviously looks like modulo operation. Now it's obvious why OptimizeFunctionOnNextCall is undefined.

    Change your code to (Notice the ;):

    let operand = 3;
    function square() {
      return operand * operand;
    }
    square(); // <-- here
    %OptimizeFunctionOnNextCall(square);
    square();
    

    Here is a nice read by T.J. Crowder (SO user) on why you should not miss ;