为什么.call和.apply比JavaScript中的直接函数调用慢? [英] Why are .call and .apply slower than a direct function call in JavaScript?

查看:100
本文介绍了为什么.call和.apply比JavaScript中的直接函数调用慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这些jsperf结果感到好奇。它们似乎证明直接函数调用比使用 .call .apply 调用的相同函数快得多。 ( .call .apply 之间的差别让我更加惊讶。)你能解释一下这些结果吗?

I'm curious about these jsperf results. They appear to demonstrate that a direct function call is substantially faster than the same function called with .call or .apply. (The difference between .call and .apply surprised me even more.) Could you please explain these results?

更新:这是一个jsperf 有人离开那个测试。应用没有第二个数组实例化。

Update: Here is a jsperf that someone left that tests .apply without a second array instantiation.

推荐答案

我猜原因可能取决于哪个翻译你正在运行代码,但似乎正常的函数调用更快,因为解释器可以使用内联缓存来访问属性。

I guess the cause might depend on which interpreter your are running the code on, but it seems that normal functions calls are faster because the interpreter can use Inline Cache to access the properties.

你可以看看这里了解更多信息。

You can have a look here for more information.

这篇关于为什么.call和.apply比JavaScript中的直接函数调用慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆