Last active
March 27, 2023 21:51
-
-
Save Llorx/975779da3919d3777877ca5b748e1a15 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Test V8 optimization against eval and new Function | |
* Run with: node --allow-natives-syntax evalFuncOpt.js | |
* More verbose with: node --trace_opt --trace_deopt --allow-natives-syntax evalFuncOpt.js | |
* | |
* More info at: https://github.com/petkaantonov/bluebird/wiki/Optimization-killers | |
*/ | |
function adder(a, b) { | |
return new Function('a', 'b', 'return b%2 ? a + b : b%3 ? a - b : b%5 ? b / a : a * b')(a, b); | |
} | |
function addereval(a, b) { | |
return eval('b%2 ? a + b : b%3 ? a - b : b%5 ? b / a : a * b'); | |
} | |
var b = 0; | |
function printStatus(fn) { | |
switch (%GetOptimizationStatus(fn)) { | |
case 1: console.log(fn.name, "function is optimized"); break; | |
case 2: console.log(fn.name, "function is not optimized"); break; | |
case 3: console.log(fn.name, "function is always optimized"); break; | |
case 4: console.log(fn.name, "function is never optimized"); break; | |
case 6: console.log(fn.name, "function is maybe deoptimized"); break; | |
case 7: console.log("Function is optimized by TurboFan"); break; | |
default: console.log("Unknown optimization status"); break; | |
} | |
} | |
adder(0, 1); | |
adder(2, 3); | |
%OptimizeFunctionOnNextCall(adder); | |
printStatus(adder); | |
adder(4, 5); | |
printStatus(adder); | |
addereval(0, 1); | |
addereval(2, 3); | |
%OptimizeFunctionOnNextCall(addereval); | |
printStatus(addereval); | |
addereval(4, 5); | |
printStatus(addereval); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment