mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 02:17:48 +00:00
Optimize _.isEqual for primitives.
This commit is contained in:
31
lodash.js
31
lodash.js
@@ -1462,7 +1462,6 @@
|
|||||||
* @returns {boolean} Returns `true` if the values are equivalent, else `false`.
|
* @returns {boolean} Returns `true` if the values are equivalent, else `false`.
|
||||||
*/
|
*/
|
||||||
function baseIsEqual(a, b, callback, isWhere, stackA, stackB) {
|
function baseIsEqual(a, b, callback, isWhere, stackA, stackB) {
|
||||||
// used to indicate that when comparing objects, `a` has at least the properties of `b`
|
|
||||||
if (callback) {
|
if (callback) {
|
||||||
var result = callback(a, b);
|
var result = callback(a, b);
|
||||||
if (typeof result != 'undefined') {
|
if (typeof result != 'undefined') {
|
||||||
@@ -1478,16 +1477,10 @@
|
|||||||
otherType = typeof b;
|
otherType = typeof b;
|
||||||
|
|
||||||
// exit early for unlike primitive values
|
// exit early for unlike primitive values
|
||||||
if (a === a &&
|
if (a === a && (a == null || b == null ||
|
||||||
!(a && (type == 'function' || type == 'object')) &&
|
(type != 'function' && type != 'object' && otherType != 'function' && otherType != 'object'))) {
|
||||||
!(b && (otherType == 'function' || otherType == 'object'))) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// exit early for `null` and `undefined` avoiding ES3's Function#call behavior
|
|
||||||
// http://es5.github.io/#x15.3.4.4
|
|
||||||
if (a == null || b == null) {
|
|
||||||
return a === b;
|
|
||||||
}
|
|
||||||
// compare [[Class]] names
|
// compare [[Class]] names
|
||||||
var className = toString.call(a),
|
var className = toString.call(a),
|
||||||
otherClass = toString.call(b);
|
otherClass = toString.call(b);
|
||||||
@@ -6025,7 +6018,24 @@
|
|||||||
* // => true
|
* // => true
|
||||||
*/
|
*/
|
||||||
function isEqual(a, b, callback, thisArg) {
|
function isEqual(a, b, callback, thisArg) {
|
||||||
return baseIsEqual(a, b, typeof callback == 'function' && baseCreateCallback(callback, thisArg, 2));
|
callback = typeof callback == 'function' && baseCreateCallback(callback, thisArg, 2);
|
||||||
|
|
||||||
|
if (!callback) {
|
||||||
|
// exit early for identical values
|
||||||
|
if (a === b) {
|
||||||
|
// treat `+0` vs. `-0` as not equal
|
||||||
|
return a !== 0 || (1 / a == 1 / b);
|
||||||
|
}
|
||||||
|
var type = typeof a,
|
||||||
|
otherType = typeof b;
|
||||||
|
|
||||||
|
// exit early for unlike primitive values
|
||||||
|
if (a === a && (a == null || b == null ||
|
||||||
|
(type != 'function' && type != 'object' && otherType != 'function' && otherType != 'object'))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return baseIsEqual(a, b, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -7107,6 +7117,7 @@
|
|||||||
if (!hasOwnProperty.call(object, key)) {
|
if (!hasOwnProperty.call(object, key)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
// treat `+0` vs. `-0` as not equal
|
||||||
var b = object[key];
|
var b = object[key];
|
||||||
return a === b && (a !== 0 || (1 / a == 1 / b));
|
return a === b && (a !== 0 || (1 / a == 1 / b));
|
||||||
};
|
};
|
||||||
|
|||||||
18
perf/perf.js
18
perf/perf.js
@@ -1268,7 +1268,23 @@
|
|||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
suites.push(
|
suites.push(
|
||||||
Benchmark.Suite('`_.isEqual` comparing primitives and objects (edge case)')
|
Benchmark.Suite('`_.isEqual` comparing primitives')
|
||||||
|
.add(buildName, {
|
||||||
|
'fn': '\
|
||||||
|
lodash.isEqual(1, "1");\
|
||||||
|
lodash.isEqual(1, 1)',
|
||||||
|
'teardown': 'function isEqual(){}'
|
||||||
|
})
|
||||||
|
.add(otherName, {
|
||||||
|
'fn': '\
|
||||||
|
_.isEqual(1, "1");\
|
||||||
|
_.isEqual(1, 1);',
|
||||||
|
'teardown': 'function isEqual(){}'
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
suites.push(
|
||||||
|
Benchmark.Suite('`_.isEqual` comparing primitives and their object counterparts (edge case)')
|
||||||
.add(buildName, {
|
.add(buildName, {
|
||||||
'fn': 'lodash.isEqual(objectOfPrimitives, objectOfObjects)',
|
'fn': 'lodash.isEqual(objectOfPrimitives, objectOfObjects)',
|
||||||
'teardown': 'function isEqual(){}'
|
'teardown': 'function isEqual(){}'
|
||||||
|
|||||||
Reference in New Issue
Block a user