Skip to content Skip to sidebar Skip to footer

How To Disable V8's Optimizing Compiler

I'm writing a constant-time string comparison function (for node.js) and would like to disable V8's optimizing compiler for this single function; using command-line flags are out o

Solution 1:

If you want solid way to do it, you need to run node with --allow-natives-syntax flag and call this:

%NeverOptimizeFunction(constantTimeStringCompare);

Note that you should call this before you have called constantTimeStringCompare, if the function is already optimized then this violates an assertion.

Otherwise with statement is your best bet as making it optimizable would be absolute lunacy whereas supporting try/catch would be reasonable. You don't need it to affect your code though, this will be sufficient:

functionconstantTimeStringCompare( a, b ) {
    with({});

    var valid = true,
        length = Math.max( a.length, b.length );
    while ( length-- ) {
        valid &= a.charCodeAt( length ) === b.charCodeAt( length );
    }
    // returns true if valid == 1, false if valid == 0return !!valid;

}

Merely mentioning with statement corrupts the entire containing function - the optimizations are done at function-level granularity, not per statement.

Solution 2:

To actually check is function optimized by particular Node.js version you can refer to Optimization Killers wiki of bluebird. I've checked 3 solutions on Node 7.2:

  1. with({}) - Function is optimized by TurboFan
  2. try {} catch(e) {} - Function is optimized by TurboFan
  3. eval(''); - Function is not optimized

So to guarantee disable V8 optimization you should add eval('') to function body.

Post a Comment for "How To Disable V8's Optimizing Compiler"