Function.prototype.bind()总是很慢吗? [英] Will Function.prototype.bind() always be slow?

查看:217
本文介绍了Function.prototype.bind()总是很慢吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个开源的javascript库,并且我大量使用 .bind()方法,因为我知道面向对象的代码看起来更清晰。 (值得商榷,但

I am writing an open source javascript library, and I use .bind() method heavily, because I have an idea that object-oriented code looks more clear then. (debatable, though)

示例

A1:

var that = this;

setTimeout(function () {
    that.method();
}, 0);

vs

B1:

setTimeout(this.method.bind(this), 0);

或者,更实用的代码部分

A2:

remoteDataSource.getData(function (a, b, c, d) {
     obj.dataGetter(a, b, c, d);
})

vs B2:

remoteDataSource.getData(obj/* or prototype */.dataGetter.bind(obj));

我使用非本地 bind 旧的浏览器,一切都很完美,直到我打开绑定的jsperf基准

I use a non-native bind for older browsers, and everything went perfect until I opened a jsperf benchmark for bind.

使用 bind 的代码看起来慢了100倍。现在,在重写我的所有库之前,我对那些熟悉javascript引擎的人有一个问题:

It looks like code using bind is 100 times slowlier. Now, before to rewrite all my library, I have a question for those who are familiar with javascript engines:

是否有可能成为新功能, bind 将很快得到优化
,或者因为JavaScript架构限制而没有机会?

推荐答案

首先,修复了jsperf http://jsperf.com/ bind-vs-emulate / 13

First of all, fixed jsperf http://jsperf.com/bind-vs-emulate/13.

=您不应在基准测试中重新创建静态函数。这是不现实的,因为在实际代码中,静态函数只创建一次。

=You should not recreate static functions inside the benchmark. That is not realistic because in real code static functions are only created once.

你可以看到 var self = this 模式仍然快约60%。但它需要内联函数定义,因为你可以从任何地方绑定,因此具有更好的可维护性。

You can see that var self = this pattern is still about 60% faster. But it requires the function definition to be inlined where as you can bind from anywhere and therefore have better maintainability.

没有,内置的绑定语义是荒谬的错综复杂。

Well no, the built-in bind semantics are ridiculously convoluted.

当我绑定时,我只想要这个:

When I bind, I just want this:

function bind(fn, ctx) {
    return function bound() {
        return fn.apply(ctx, arguments);
    };
}

如果我想预先应用参数或使用一些深层构造黑魔法,我想要一个完全不同的功能。我不知道为什么任何这个都包含在bind中。

If I wanted to pre-apply arguments or use some deep constructor black magic, I would want a totally different function for that. I have no idea why any of this was included in bind.

< rant>顺便说一句,同样的问题出现在ES5中引入的几乎所有内容中,通过强制实现处理一些不合理相关的理论边缘情况来惩罚常见情况在实践中的任何人。下一个语言版本继续在同一条路径上。< / rant>

<rant>Btw, the same problem is with almost anything introduced in ES5, punishing the common case by forcing implementations to handle some theoretical edge case that is not plausibly relevant to anyone in practice. The next language version is continuing on the same path.</rant>

模拟绑定甚至根本不会尝试模拟绑定。即使你试图模仿它,你也不能完全做到b $ b b这样做是不公平的。

The emulated bind doesn't even try to emulate bind at all. And even if you try to emulate it, you will not be able to do it completely so that's just not fair.

所以其他一切都等于*内置绑定不能比只是绑定的常识自定义绑定更快。

So with everything else equal* the built-in bind cannot be faster than common sense custom bind which just binds.

*在JIT中,用户代码对内置代码没有明显的缺点。事实上,SM和V8都在Javascript中实现了许多内置

*In JITs user code has no significant disadvantage to built-in code. In fact both SM and V8 implement many built-ins in Javascript.

这篇关于Function.prototype.bind()总是很慢吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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