Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

call和apply的性能对比 #6

Open
noneven opened this issue Oct 25, 2016 · 7 comments
Open

call和apply的性能对比 #6

noneven opened this issue Oct 25, 2016 · 7 comments

Comments

@noneven
Copy link
Owner

noneven commented Oct 25, 2016

以前看jQuery源码的时候有看到在源码的注释中有些过call的性能会比apply好,在lodash的源码中也同样的发现有call比apply性能更好的注释,这里我在jsperf上写了几个test case,验证了一下确实call比apply的性能更好。

  • 0、lodash源码apply方法重写
  • 1、无指向无参数对比:
  • 2、有指向无参数对比:
  • 3、无参数有指向:
  • 4、有参数有指向对比:

总结: 在我们平时的开发中其实不必关注call和apply的性能问题,但是可以尽可能的去用call,特别是es6的reset解构的支持,call基本可以代替apply,可以看出lodash源码里面并没有直接用Function.prototype.apply,而是在参数较少(1-3)个时采用call的方式调用(因为lodash里面没有超过4个参数的方法,PS如果一个函数的设计超过4个入参,那么这个函数就要考虑重构了)

转载请注明出处

@Been101
Copy link

Been101 commented Aug 7, 2019

这是用什么工具测试的

以前看jQuery源码的时候有看到在源码的注释中有些过call的性能会比apply好,在lodash的源码中也同样的发现有call比apply性能更好的注释,这里我在jsperf上写了几个test case,验证了一下确实call比apply的性能更好。

  • 0、lodash源码apply方法重写
  • 1、无指向无参数对比:
  • 2、有指向无参数对比:
  • 3、无参数有指向:
  • 4、有参数有指向对比:

总结: 在我们平时的开发中其实不必关注call和apply的性能问题,但是可以尽可能的去用call,特别是es6的reset解构的支持,call基本可以代替apply,可以看出lodash源码里面并没有直接用Function.prototype.apply,而是在参数较少(1-3)个时采用call的方式调用(因为lodash里面没有超过4个参数的方法,PS如果一个函数的设计超过4个入参,那么这个函数就要考虑重构了)

转载请注明出处

这是用什么工具测试的

@uboa3774
Copy link

uboa3774 commented Aug 23, 2019

这是用什么工具测试的

这…人家写了用jsperf

@CodingYiyi
Copy link

总结: 在我们平时的开发中其实不必关注call和apply的性能问题,但是可以尽可能的去用call,特别是es6的reset解构的支持,call基本可以代替apply,可以看出lodash源码里面并没有直接用Function.prototype.apply,而是在参数较少(1-3)个时采用call的方式调用(因为lodash里面没有超过4个参数的方法,PS如果一个函数的设计超过4个入参,那么这个函数就要考虑重构了)

函数参数超过3个情况, lodash 源码中统一按照原始的apply处理了,源码 最后一行的 return

@yudekai
Copy link

yudekai commented Apr 29, 2020

不靠谱!!!!!!

image

@elixiao
Copy link

elixiao commented Jun 17, 2020

为什么我得到的是相反的结果?call 比 apply 慢 5 倍以上。

const arr= Array.from({ length: 100000 })

console.time('call')
console.log(Array.prototype.push.call([], ...arr))
console.timeEnd('call')

console.time('apply')
console.log(Array.prototype.push.apply([], arr))
console.timeEnd('apply')
$ node call-vs-apply.js
100000
call: 19.790ms
100000
apply: 3.907ms

@alan1111
Copy link

为什么我得到的是相反的结果?call 比 apply 慢 5 倍以上。

const arr= Array.from({ length: 100000 })

console.time('call')
console.log(Array.prototype.push.call([], ...arr))
console.timeEnd('call')

console.time('apply')
console.log(Array.prototype.push.apply([], arr))
console.timeEnd('apply')
$ node call-vs-apply.js
100000
call: 19.790ms
100000
apply: 3.907ms

好尴尬,我测试的结果也是apply更快点. 而且跟传入参数个数无关,都是apply更快点.

@daiwiy
Copy link

daiwiy commented Jul 6, 2020

为什么我得到的是相反的结果?call 比 apply 慢 5 倍以上。

const arr= Array.from({ length: 100000 })

console.time('call')
console.log(Array.prototype.push.call([], ...arr))
console.timeEnd('call')

console.time('apply')
console.log(Array.prototype.push.apply([], arr))
console.timeEnd('apply')
$ node call-vs-apply.js
100000
call: 19.790ms
100000
apply: 3.907ms

有点看不懂,不过我把你的长度改成3和10分别拿了100次数据,
call都要快一点

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants