Skip to content

Commit

Permalink
Improve Array checks to avoid checking Object#toString when possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jan 28, 2015
1 parent bc96071 commit 4617222
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ var isString = function isString(value) {
return false;
}
};
var arrayType = '[object Array]';

var isFunction = function (value) {
try {
fnToStr.call(value);
Expand All @@ -63,12 +61,15 @@ var isFunction = function (value) {
}
};

var isArray = Array.isArray || function (value) {
return toStr.call(value) === '[object Array]';
};

module.exports = function isEqual(value, other) {
if (value === other) { return true; }
if (value == null || other == null) { return value === other; }

var type = toStr.call(value);
if (type !== toStr.call(other)) { return false; }
if (toStr.call(value) !== toStr.call(other)) { return false; }

var valIsBool = isBoolean(value);
var otherIsBool = isBoolean(other);
Expand Down Expand Up @@ -100,7 +101,10 @@ module.exports = function isEqual(value, other) {
return valIsRegex && otherIsRegex && String(value) === String(other);
}

if (type === arrayType) {
var valIsArray = isArray(value);
var otherIsArray = isArray(other);
if (valIsArray || otherIsArray) {
if (!valIsArray || !otherIsArray) { return false; }
if (value.length !== other.length) { return false; }
if (String(value) !== String(other)) { return false; }

Expand Down

0 comments on commit 4617222

Please sign in to comment.