Last active
December 13, 2015 20:17
-
-
Save jhurliman/3aaf609ee79aa756b58a to your computer and use it in GitHub Desktop.
Use V8 internal methods to test if a JS function can be optimized
This file contains hidden or 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
/** | |
* Based on code from <https://github.com/petkaantonov/bluebird/wiki/Optimization-killers> | |
* Run this script using: | |
* `node --allow-natives-syntax optimization-test.js` | |
* For more diagnostic output use: | |
* `node --trace_opt --trace_deopt --allow-natives-syntax optimization-test.js` | |
*/ | |
function myFunction() { | |
return 'hello world'; | |
} | |
function printStatus(fn) { | |
fn(); | |
fn(); | |
%OptimizeFunctionOnNextCall(fn); | |
fn(); | |
switch (%GetOptimizationStatus(fn)) { | |
case 1: success('Function is optimized'); break; | |
case 2: fail('Function is not optimized'); break; | |
case 3: success('Function is always optimized'); break; | |
case 4: fail('Function is never optimized'); break; | |
case 6: fail('Function is maybe deoptimized'); break; | |
case 7: success('Function is optimized by TurboFan'); break; | |
default: fail('Unknown optimization status'); break; | |
} | |
} | |
function success(msg) { | |
console.log('\033[32m' + msg + '\033[0m'); | |
} | |
function fail(msg) { | |
console.warn('\033[31m' + msg + '\033[0m'); | |
} | |
printStatus(myFunction); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment