Why lodash uses switch-case in negate function? [duplicate]

痞子三分冷 提交于 2019-12-22 18:34:15

问题


The following code is the source code of negate function in lodash. As we can see that when the length of parameters less than 4, it uses switch-case instead of using apply directly. What magic about this code? Does it make the performance better? And why the split point is 4?

function negate(predicate) {
  if (typeof predicate != 'function') {
    throw new TypeError(FUNC_ERROR_TEXT);
  }
  return function() {
    var args = arguments;
    switch (args.length) {
      case 0: return !predicate.call(this);
      case 1: return !predicate.call(this, args[0]);
      case 2: return !predicate.call(this, args[0], args[1]);
      case 3: return !predicate.call(this, args[0], args[1], args[2]);
    }
    return !predicate.apply(this, args);
  };
}

回答1:


Sry, but my crystal ball is still in repair. So I can only give you my best guess about the intentions of the author.

I think the point here is not about the switch but rather to enable the optimizer to convert this whole construct into a a single one of these paths, assuming the function will be called with a consistent set of arguments.

basically optimizing this whole constuct away and even inlining the function body of the predicate into something like this pseudocode.
assuming the function was always called with 2 arguments

function(){
    //a gate to ensure the optimization is still correct/applicable
    //probably more complex than this
    if(arguments.length === 2){          
        //assign predicate function args
        var value = arguments[0], index = arguments[1];
        [function body of predicate]
    }else{
        [de-optimize]
        //execute the de-optimized version of this function
    }
}

Why 0..3 arguments? imo, these are the most common cases. And the default case is only there for completeness and (again, my opinion) should never/rarely been hit.

Another point may be, that Function#call() is in my experience a bit faster than Function#apply(). Rarely enough to even think about it, but this is a lib that will be used by other libraries, so every performance issue may increase exponetially.



来源:https://stackoverflow.com/questions/47693024/why-lodash-uses-switch-case-in-negate-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!