Understanding the fast alternative apply() in lodash

≯℡__Kan透↙ 提交于 2019-11-27 16:30:16
Shaun

You would need to bench test speed differences to be sure.

See this SO post on speed differences between call and apply:

Why is call so much faster than apply?

So it's not really a "faster" apply, it just executes call if it can.

It will take more than 3 arguments, the last line is a catch all that calls the standard apply.

Presumably _lodash has considered that having a huge long switch determining how many arguments are passed in defeats the purpose and decided to limit it to three.

The simple answer: it's optimizing for the common case. The fastest path here is to call a function without any arguments (it's the first case in the switch). As it turns out, that's the most common way to call a function. The next most common calls are with 1, 2, and 3 arguments.

Functions called with 4+ arguments are rare enough that there's no justification to add more code here.

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