Chrome 59 更新:正如我在下面的答案中预测的那样,使用新的优化编译器,bind 不再变慢。这是包含详细信息的代码:https : //codereview.chromium.org/2916063002/
大多数时候这无关紧要。
除非你正在创建一个应用程序,否则.bind
我不会打扰。在大多数情况下,可读性比纯粹的性能重要得多。我认为使用本机.bind
通常会提供更具可读性和可维护性的代码 - 这是一个很大的优势。
但是,是的,重要的时候 - .bind
速度较慢
是的,.bind
比闭包慢得多——至少在 Chrome 中,至少在目前它在v8
. 我个人有时不得不为性能问题切换到 Node.JS(更一般地说,闭包在性能密集的情况下有点慢)。
为什么?因为该.bind
算法比用另一个函数包装一个函数并使用.call
or复杂得多.apply
。(有趣的是,它还返回一个将 toString 设置为 [native function] 的函数)。
有两种方式来看待这一点,从规范的角度和从实现的角度来看。让我们观察两者。
- 让 Target 成为这个值。
- 如果 IsCallable(Target) 为 false,则抛出 TypeError 异常。
- 令 A 是一个新的(可能为空的)内部列表,其中包含在 thisArg(arg1、arg2 等)之后提供的所有参数值,按顺序排列。
...
(21.调用F的[[DefineOwnProperty]]内部方法,参数为“arguments”,PropertyDescriptor {[[Get]]: thrower, [[Set]]: thrower, [[Enumerable]]: false, [[Configurable] ]:假},假。
(22. 返回 F。
看起来很复杂,不仅仅是一个包装。
让我们检查FunctionBind
一下 v8(chrome JavaScript 引擎)源代码:
function FunctionBind(this_arg) { // Length is 1.
if (!IS_SPEC_FUNCTION(this)) {
throw new $TypeError('Bind must be called on a function');
}
var boundFunction = function () {
// Poison .arguments and .caller, but is otherwise not detectable.
"use strict";
// This function must not use any object literals (Object, Array, RegExp),
// since the literals-array is being used to store the bound data.
if (%_IsConstructCall()) {
return %NewObjectFromBound(boundFunction);
}
var bindings = %BoundFunctionGetBindings(boundFunction);
var argc = %_ArgumentsLength();
if (argc == 0) {
return %Apply(bindings[0], bindings[1], bindings, 2, bindings.length - 2);
}
if (bindings.length === 2) {
return %Apply(bindings[0], bindings[1], arguments, 0, argc);
}
var bound_argc = bindings.length - 2;
var argv = new InternalArray(bound_argc + argc);
for (var i = 0; i < bound_argc; i++) {
argv[i] = bindings[i + 2];
}
for (var j = 0; j < argc; j++) {
argv[i++] = %_Arguments(j);
}
return %Apply(bindings[0], bindings[1], argv, 0, bound_argc + argc);
};
%FunctionRemovePrototype(boundFunction);
var new_length = 0;
if (%_ClassOf(this) == "Function") {
// Function or FunctionProxy.
var old_length = this.length;
// FunctionProxies might provide a non-UInt32 value. If so, ignore it.
if ((typeof old_length === "number") &&
((old_length >>> 0) === old_length)) {
var argc = %_ArgumentsLength();
if (argc > 0) argc--; // Don't count the thisArg as parameter.
new_length = old_length - argc;
if (new_length < 0) new_length = 0;
}
}
// This runtime function finds any remaining arguments on the stack,
// so we don't pass the arguments object.
var result = %FunctionBindArguments(boundFunction, this,
this_arg, new_length);
// We already have caller and arguments properties on functions,
// which are non-configurable. It therefore makes no sence to
// try to redefine these as defined by the spec. The spec says
// that bind should make these throw a TypeError if get or set
// is called and make them non-enumerable and non-configurable.
// To be consistent with our normal functions we leave this as it is.
// TODO(lrn): Do set these to be thrower.
return result;
我们可以在实现中看到一堆昂贵的东西。即%_IsConstructCall()
。这当然是遵守规范所必需的——但在许多情况下,这也使得它比简单的包装更慢。
另一方面,调用.bind
也略有不同,规范指出“使用 Function.prototype.bind 创建的函数对象没有原型属性或 [[Code]]、[[FormalParameters]] 和 [[Scope]] 内部特性”