From 0a9a3e855cf9e1b5415a0e7702e053abe0d71902 Mon Sep 17 00:00:00 2001 From: Carl-Erik Kopseng Date: Thu, 10 Aug 2017 12:18:15 +0200 Subject: [PATCH] Cache other references to Array.prototype.filter --- lib/sinon/call.js | 5 +++-- lib/sinon/collection.js | 5 +++-- lib/sinon/mock.js | 5 +++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/sinon/call.js b/lib/sinon/call.js index a9484ca54..9d698e56b 100644 --- a/lib/sinon/call.js +++ b/lib/sinon/call.js @@ -6,6 +6,7 @@ var functionName = require("./util/core/function-name"); var sinonFormat = require("./util/core/format"); var valueToString = require("./util/core/value-to-string"); var slice = Array.prototype.slice; +var filter = Array.prototype.filter; function throwYieldError(proxy, text, args) { var msg = functionName(proxy) + text; @@ -131,7 +132,7 @@ var callProto = { yieldOn: function (thisValue) { var args = slice.call(this.args); - var yieldFn = args.filter(function (arg) { + var yieldFn = filter.call(args, function (arg) { return typeof arg === "function"; })[0]; @@ -148,7 +149,7 @@ var callProto = { yieldToOn: function (prop, thisValue) { var args = slice.call(this.args); - var yieldArg = args.filter(function (arg) { + var yieldArg = filter.call(args, function (arg) { return arg && typeof arg[prop] === "function"; })[0]; var yieldFn = yieldArg && yieldArg[prop]; diff --git a/lib/sinon/collection.js b/lib/sinon/collection.js index 0f369db3f..7a487a20c 100644 --- a/lib/sinon/collection.js +++ b/lib/sinon/collection.js @@ -6,7 +6,8 @@ var sinonMock = require("./mock"); var collectOwnMethods = require("./collect-own-methods"); var valueToString = require("./util/core/value-to-string"); -var push = [].push; +var push = Array.prototype.push; +var filter = Array.prototype.filter; function getFakes(fakeCollection) { if (!fakeCollection.fakes) { @@ -18,7 +19,7 @@ function getFakes(fakeCollection) { function each(fakeCollection, method) { var fakes = getFakes(fakeCollection); - var matchingFakes = fakes.filter(function (fake) { + var matchingFakes = filter.call(fakes, function (fake) { return typeof fake[method] === "function"; }); diff --git a/lib/sinon/mock.js b/lib/sinon/mock.js index 3b4f37318..54084d8d0 100644 --- a/lib/sinon/mock.js +++ b/lib/sinon/mock.js @@ -8,6 +8,7 @@ var deepEqual = require("./util/core/deep-equal").use(match); var wrapMethod = require("./util/core/wrap-method"); var push = Array.prototype.push; +var filter = Array.prototype.filter; function mock(object) { if (!object || typeof object === "string") { @@ -118,13 +119,13 @@ extend(mock, { var currentArgs = args || []; var available; - var expectationsWithMatchingArgs = expectations.filter(function (expectation) { + var expectationsWithMatchingArgs = filter.call(expectations, function (expectation) { var expectedArgs = expectation.expectedArguments || []; return arrayEquals(expectedArgs, currentArgs, expectation.expectsExactArgCount); }); - var expectationsToApply = expectationsWithMatchingArgs.filter(function (expectation) { + var expectationsToApply = filter.call(expectationsWithMatchingArgs, function (expectation) { return !expectation.met() && expectation.allowsCall(thisValue, args); });