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)
Example
A1:
var that = this;
setTimeout(function () {
that.method();
}, 0);
vs
B1:
setTimeout(this.method.bind(this), 0);
Or, a more practical code portion
A2:
remoteDataSource.getData(function (a, b, c, d) {
obj.dataGetter(a, b, c, d);
})
vs B2:
remoteDataSource.getData(obj/* or prototype */.dataGetter.bind(obj));
I use a non-native bind
for older browsers, and everything went perfect until I opened a jsperf benchmark for bind.
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:
Is there a probability that, being a new feature, bind
will get optimized
soon, or there is no chance because of JavaScript architecture limits?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…