In short, the call to eval inside a function and the ability to access the arguments array take advantage of additional configuration during function calls. If it is known that neither arguments nor eval will be executed, this additional configuration may be skipped.
The compiler does not try to predict whether access to the arguments array will really be available or whether eval will actually be called, it checks whether or not they exist in the function.
arguments
At run time, at run time, a function variable that uses the arguments object is called more expensively than a "normal" function that does not use the arguments object.
Additional steps necessary to bind the runtime when declaring an arguments object are: specified in ยง10.6 of the ECMA-262 standard . Creating an arguments object is a somewhat expensive 15-step process. Basically, arguments should be filled with arguments with a pass, and the .caller and .callee should be created.
The standard states that an arguments object should be created when a function enters the execution context if there is no longer a parameter, variable, or function in the function named arguments .
For optimization purposes, most browsers do not actually create an arguments object unless the function actually uses it somewhere (even after return ). That's why you see a performance hit when arguments referenced, even if the lines containing it are never executed.
eval
Entering eval code, as specified in ยง10.4.2 of the ECMA-262 standard , requires the creation of a special execution context. Basically, it should bind all properties of the execution context of the calling function to the eval context.
If multiple eval is called in the same function, they will basically execute the same process twice. To optimize, if browsers find that there is an eval function in the function (even after return ), it pre-populates this new execution context that every eval can use, so it does not need to be recreated several times.
Please note that these optimizations are browser-dependent and not required by the standard ones , so some browsers may not perform the described optimizations, otherwise they may do something differently.