Skip to content

Instantly share code, notes, and snippets.

@joewagner
Created February 2, 2016 20:06
Show Gist options
  • Save joewagner/d89ff977e47e039387f0 to your computer and use it in GitHub Desktop.
Save joewagner/d89ff977e47e039387f0 to your computer and use it in GitHub Desktop.
Demonstrates v8 optimization for reassigning vs. not reassigning `arguments` object
// reassigning an argument
function argReassign1(a) {
a = a || {};
}
// not reassigning an argument
function argReassign2(a) {
if (a === void 0) var a = {};
}
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;
case 7: console.log("Function is optimized by TurboFan"); break;
default: console.log("Unknown optimization status"); break;
}
}
// 2 calls each are needed to go from uninitialized -> pre-monomorphic -> monomorphic
argReassign1({});
argReassign2({});
argReassign1({});
argReassign2({});
%OptimizeFunctionOnNextCall(argReassign1);
//The next call
argReassign1()
console.log('argReassign1:');
//Check
printStatus(argReassign1);
%OptimizeFunctionOnNextCall(argReassign2);
//The next call
argReassign2()
console.log('argReassign2:');
//Check
printStatus(argReassign2);
@joewagner
Copy link
Author

To run this you will have to use the allow_natives_syntax flag
e.g.

node --allow_natives_syntax optimization-status.js

@scurker
Copy link

scurker commented Feb 2, 2016

Dug in a little further with --trace-opt, but doesn't seem like there's any bailout reason specifically for argReassign1.

[optimizing 0x37b63088099 <JS Function argReassign1 (SharedFunctionInfo 0x337c8530e461)> - took 0.022, 0.058, 0.014 ms]
[evicting entry from optimizing code map (notify deoptimized) for 0x337c8530e461 <SharedFunctionInfo argReassign1>]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment