Performance penalty for undefined arguments

前端 未结 2 641
逝去的感伤
逝去的感伤 2021-02-08 09:41

I quite often have optional arguments in functions, but some testing is showing a huge performance hit for them in firefox and safari (70-95%). Strangely, if I pass in the liter

2条回答
  •  再見小時候
    2021-02-08 10:14

    For a function like this:

    function threeArgs(x, y, z) {
      return x + y + z;
    }
    

    that's called like this:

    threeArgs(1, 2, 3);
    

    the optimizer is free to make the choice to generate no code at all. It's fairly easy for it to determine that there are no side effects, because the function simply references its parameter values and returns the result of a simple expression. Since the return value is ignored, there's no reason for the runtime to do anything at all.

    Beyond that, if the code were:

    something += threeArgs(1, 2, 3);
    

    the optimizer might decide to generate code roughly equivalent to:

    something += 6;
    

    Why? Because the call was made with numeric constants, and it can safely fold those at code generation time. It might be conservative on that, because numbers are weird, but here they're all integers so it could well do this. Even if it didn't, it could safely inline the function:

    something += 1 + 2 + 3;
    

    When there's a parameter missing, however, it may be that the optimizers bail out and generate a real function call. For such a simple function, the overhead of the function call could easily account for a large difference in performance.

    By using variables instead of constants in a test, and by actually using the return value of the function, you can "confuse" the optimizer and keep it from skipping the call or pre-computing the result, but you can't keep it from inlining. I still think that your result is interesting for that reason: it exposes the fact that (as of today anyway) those optimizers are sensitive to the way that functions are invoked.

提交回复
热议问题