Don't construct unnecessarily asymmetricEqualityTesterArgCompatShims

This speeds up MatchersUtil#equals by about 6-7x.
This commit is contained in:
Steve Gravrock
2020-02-12 13:52:58 -08:00
parent 3be797c8d8
commit 5096d9af4e
4 changed files with 31 additions and 6 deletions

View File

@@ -279,7 +279,7 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
};
j$.isAsymmetricEqualityTester_ = function(obj) {
return obj && j$.isA_('Function', obj.asymmetricMatch);
return obj ? j$.isA_('Function', obj.asymmetricMatch) : false;
};
j$.getType_ = function(value) {
@@ -4494,13 +4494,15 @@ getJasmineRequireObj().MatchersUtil = function(j$) {
MatchersUtil.prototype.asymmetricMatch_ = function(a, b, aStack, bStack, customTesters, diffBuilder) {
var asymmetricA = j$.isAsymmetricEqualityTester_(a),
asymmetricB = j$.isAsymmetricEqualityTester_(b),
shim = j$.asymmetricEqualityTesterArgCompatShim(this, customTesters),
shim,
result;
if (asymmetricA && asymmetricB) {
if (asymmetricA === asymmetricB) {
return undefined;
}
shim = j$.asymmetricEqualityTesterArgCompatShim(this, customTesters);
if (asymmetricA) {
result = a.asymmetricMatch(b, shim);
if (!result) {

View File

@@ -30,4 +30,25 @@ describe('base helpers', function() {
}, 100);
});
});
describe('isAsymmetricEqualityTester_', function() {
it('returns false when the argument is falsy', function() {
expect(jasmineUnderTest.isAsymmetricEqualityTester_(null)).toBe(false);
});
it('returns false when the argument does not have a asymmetricMatch property', function() {
var obj = {};
expect(jasmineUnderTest.isAsymmetricEqualityTester_(obj)).toBe(false);
});
it("returns false when the argument's asymmetricMatch is not a function", function() {
var obj = { asymmetricMatch: 'yes' };
expect(jasmineUnderTest.isAsymmetricEqualityTester_(obj)).toBe(false);
});
it("returns true when the argument's asymmetricMatch is a function", function() {
var obj = { asymmetricMatch: function() {} };
expect(jasmineUnderTest.isAsymmetricEqualityTester_(obj)).toBe(true);
});
});
});

View File

@@ -112,7 +112,7 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
};
j$.isAsymmetricEqualityTester_ = function(obj) {
return obj && j$.isA_('Function', obj.asymmetricMatch);
return obj ? j$.isA_('Function', obj.asymmetricMatch) : false;
};
j$.getType_ = function(value) {

View File

@@ -90,13 +90,15 @@ getJasmineRequireObj().MatchersUtil = function(j$) {
MatchersUtil.prototype.asymmetricMatch_ = function(a, b, aStack, bStack, customTesters, diffBuilder) {
var asymmetricA = j$.isAsymmetricEqualityTester_(a),
asymmetricB = j$.isAsymmetricEqualityTester_(b),
shim = j$.asymmetricEqualityTesterArgCompatShim(this, customTesters),
shim,
result;
if (asymmetricA && asymmetricB) {
if (asymmetricA === asymmetricB) {
return undefined;
}
shim = j$.asymmetricEqualityTesterArgCompatShim(this, customTesters);
if (asymmetricA) {
result = a.asymmetricMatch(b, shim);
if (!result) {