Skip to content

Instantly share code, notes, and snippets.

@yuvalbl
Last active September 30, 2018 06:00
Show Gist options
  • Save yuvalbl/2f9032d71a69288e2d21e93ae0055f3c to your computer and use it in GitHub Desktop.
Save yuvalbl/2f9032d71a69288e2d21e93ae0055f3c to your computer and use it in GitHub Desktop.
Test cases on v8
// run with :
// node --trace_deopt --allow-natives-syntax FILENAME
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');
}
function printStatus(fn) {
var status = %GetOptimizationStatus(fn)
switch (status) {
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(fn.name,"Function is optimized by TurboFan"); break;
default: console.log(fn.name, "Unknown optimization status: ", status); break;
}
}
printStatus(adder);
printStatus(addereval);
for(let i = 0; i < 263; i++) {
adder(1, 2);
}
console.log('\n', '==== adder after invocation ===');
printStatus(adder);
addereval(1, 2);
console.log('\n', '==== addereval after invocation ===');
printStatus(addereval);
/*
tested on windows7 with node v8.2.1 (v8 version 5.8.283.41) And
linux ubuntu with node v8.7.0(v8 version 6.1.534.42) :
output values:
--------------
adder function is optimized
addereval function is optimized
==== adder after invocation ===
adder Unknown optimization status: 17 (STATUS IS 65 ON NODE V8.7.0)
==== addereval after invocation ===
addereval Unknown optimization status: 65
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment