-
-
Save yuche/058dc6e58d34cee43f7c7d366d6b8111 to your computer and use it in GitHub Desktop.
检测 js 是否被 V8 优化
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
/** | |
* 执行语句: node --trace_opt --trace_deopt --allow-natives-syntax test.js | |
*/ | |
// 包含需要审查的用法的函数 (这里是 with 语句) | |
function containsHeight() { | |
return 3; | |
// with({}) { } // with 注释掉之后,又可以优化了 | |
} | |
// 以下为检验工具 | |
function printStatus(fn) { | |
switch(%GetOptimizationStatus(fn)) { | |
case 1: console.log("Function is optimized"); break; | |
case 2: console.log("Function is not optimized"); break; | |
case 3: console.log("Function is always optimized"); break; | |
case 4: console.log("Function is never optimized"); break; | |
case 6: console.log("Function is maybe deoptimized"); break; | |
} | |
} | |
// 告诉编译器类型信息 | |
containsHeight(); | |
// 为了使状态从 uninitialized 变为 pre-monomorphic, 再变为 monomorphic, 两次调用是必要的 | |
containsHeight(); | |
%OptimizeFunctionOnNextCall(containsHeight); | |
// 下一次调用 | |
containsHeight(); | |
// 检查 | |
printStatus(containsHeight); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment