class TestClass { megaFunc() { try { let sum = 0; for (let val of [1, 2, 3]) { sum += val; } throw new Error(`sync error, sum = ${sum}`); } catch(err) { return err; } } } let test = new TestClass(); checkOptimizationStatus(test.megaFunc);
Function is optimized by TurboFan
Currently not optimized:
- function generators;
- functions containing a for-of expression;
- functions containing a try-catch expression;
- functions containing a try-finally statement;
- functions containing a compound let assignment statement;
- functions containing the const compound assignment operator;
- functions that contain object literals, which in turn contain __proto__, get, or set declarations.
Most likely, not optimized:
- functions containing a debugger expression;
- functions that call eval ();
- functions containing a with expression.
There are many ways to use arguments so that it will not be possible to optimize a function. So when working with arguments you should be especially careful.
...
The switch-case expression today can have up to 128 case points, and if this number is exceeded, the function containing this expression cannot be optimized.
A For-in expression can interfere with function optimization in several ways. 5.1. The key is not a local variable.
5.2. The object being iterated is not “simple enumerable”.
5.2.2. There are fields with enumerated values ​​in the object prototype chain.
5.2.3. The object contains enumerated array indices.
var key; function nonLocalKey2() { var obj = {} for(key in obj); }
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --js-flags="--allow-natives-syntax"
<script src="index.js"></script>
function exampleFunction() { return 3; eval(''); } checkOptimizationStatus(exampleFunction) function checkOptimizationStatus(exampleFunction) { exampleFunction(); exampleFunction(); %OptimizeFunctionOnNextCall(exampleFunction); exampleFunction(); switch (%GetOptimizationStatus(exampleFunction)) { 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; case 7: console.log("Function is optimized by TurboFan " + exampleFunction.name); break; case 49: console.log("Function is optimized by NewMethod " + exampleFunction.name); break; default: console.log("Unknown optimization status"); break; } }
node --allow-natives-syntax index.js
async function delayAsync(delay) { return new Promise(resolve => { setTimeout(() => resolve(), delay) }) } async function asyncTest() { return 'habrahabr' } async function exampleFunction() { try { let result = await asyncTest() await delayAsync(500) console.log(`result after 500ms: ${result}`) } catch (err) { console.error(err) } }
Function is optimized by TurboFan: exampleFunction Function is optimized by TurboFan: asyncTest Function is optimized by TurboFan: delayAsync // 500ms, (3) after 500ms: habrahabr
Source: https://habr.com/ru/post/319936/