Skip to content

Instantly share code, notes, and snippets.

@yuche
Forked from kreja/GetOptimizationStatus
Last active December 21, 2017 06:09
Show Gist options
  • Save yuche/058dc6e58d34cee43f7c7d366d6b8111 to your computer and use it in GitHub Desktop.
Save yuche/058dc6e58d34cee43f7c7d366d6b8111 to your computer and use it in GitHub Desktop.
检测 js 是否被 V8 优化
/**
* 执行语句: 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