理解lodash中的快速替代apply() [英] Understanding the fast alternative apply() in lodash
问题描述
我阅读了 lo-dash 的源代码,并找到了函数快速替代 apply()
此处。
I read the source code of lo-dash, and find there is fast alternative the function apply()
here.
function apply(func, thisArg, args) {
switch (args.length) {
case 0: return func.call(thisArg);
case 1: return func.call(thisArg, args[0]);
case 2: return func.call(thisArg, args[0], args[1]);
case 3: return func.call(thisArg, args[0], args[1], args[2]);
}
return func.apply(thisArg, args);
}
我想知道的是实现快速替代功能的真正有效方式应用()
?为什么在这里分解不超过3个args?
I want to know is that really efficient way to implement the fast alternative function apply()
? Why there is no more than 3 args to decomposed here?
推荐答案
你需要测试速度差异才能确定。
You would need to bench test speed differences to be sure.
请参阅此SO帖子,了解电话和申请之间的速度差异:
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.
它将需要超过3个参数,最后一行是一个所有调用标准apply的函数。
It will take more than 3 arguments, the last line is a catch all that calls the standard apply.
据推测,_lodash认为有一个巨大的长开关确定传递了多少个参数会使目的失败,并决定将其限制为三个。
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.
这篇关于理解lodash中的快速替代apply()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!