Skip to content

Commit b504f6a

Browse files
committed
perf: improve performance on some primitives
1 parent 5c442a5 commit b504f6a

1 file changed

Lines changed: 25 additions & 5 deletions

File tree

index.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,32 @@ module.exports.MemoizeMap = MemoizeMap;
9494
* @return {Boolean} equal match
9595
*/
9696

97+
// Fast comparisons go through this small function for optimisation
9798
function deepEqual(leftHandOperand, rightHandOperand, options) {
99+
var leftHandType = typeof leftHandOperand;
100+
if (
101+
leftHandOperand === null ||
102+
leftHandOperand === undefined || // eslint-disable-line no-undefined
103+
leftHandType === 'boolean' ||
104+
leftHandType === 'string'
105+
) {
106+
return leftHandOperand === rightHandOperand;
107+
}
108+
109+
if (leftHandType !== typeof rightHandOperand) {
110+
return false;
111+
}
112+
113+
// Deeper comparisons are pushed through to a larger function
114+
return extensiveDeepEqual(leftHandOperand, rightHandOperand, options);
115+
}
116+
117+
function extensiveDeepEqual(leftHandOperand, rightHandOperand, options) {
118+
var result = objectIs(leftHandOperand, rightHandOperand);
119+
if (result) {
120+
return true;
121+
}
122+
98123
options = options || {};
99124
options = {
100125
comparator: options.comparator || objectIs,
@@ -104,11 +129,6 @@ function deepEqual(leftHandOperand, rightHandOperand, options) {
104129
options.memoize = new MemoizeMap();
105130
}
106131

107-
var result = objectIs(leftHandOperand, rightHandOperand);
108-
if (result) {
109-
return true;
110-
}
111-
112132
var memoizeResult = memoizeCompare(leftHandOperand, rightHandOperand, options.memoize);
113133
if (typeof memoizeResult === 'boolean') {
114134
return memoizeResult;

0 commit comments

Comments
 (0)