From ee0ca0db349882544c92ee37428ab040baba24c3 Mon Sep 17 00:00:00 2001 From: Vlad Balin Date: Sun, 17 May 2015 00:29:57 +0300 Subject: [PATCH 1/3] optimized isEqual --- underscore.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/underscore.js b/underscore.js index 1efa346b5..8ad920d32 100644 --- a/underscore.js +++ b/underscore.js @@ -1203,7 +1203,10 @@ // Perform a deep comparison to check if two objects are equal. _.isEqual = function(a, b) { - return eq(a, b); + var typeA = typeof a; + if( typeA != typeof b ) return false; + if( typeA == 'object' ) return a && b ? eq( a, b ) : a === b; + return a === 0 || a != a ? eq(a, b) : a === b; }; // Is a given array, string, or object empty? From 37eef9d1f9b1ea350ac329d514a005cb4f9612a4 Mon Sep 17 00:00:00 2001 From: Vlad Balin Date: Sun, 17 May 2015 00:45:17 +0300 Subject: [PATCH 2/3] fixed performance problem with 0 --- underscore.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/underscore.js b/underscore.js index 8ad920d32..6d8bb7164 100644 --- a/underscore.js +++ b/underscore.js @@ -1110,11 +1110,6 @@ // Internal recursive comparison function for `isEqual`. var eq = function(a, b, aStack, bStack) { - // Identical objects are equal. `0 === -0`, but they aren't identical. - // See the [Harmony `egal` proposal](http://wiki.ecmascript.org/doku.php?id=harmony:egal). - if (a === b) return a !== 0 || 1 / a === 1 / b; - // A strict comparison is necessary because `null == undefined`. - if (a == null || b == null) return a === b; // Unwrap any wrapped objects. if (a instanceof _) a = a._wrapped; if (b instanceof _) b = b._wrapped; @@ -1203,6 +1198,9 @@ // Perform a deep comparison to check if two objects are equal. _.isEqual = function(a, b) { + // Identical objects are equal. `0 === -0`, but they aren't identical. + // See the [Harmony `egal` proposal](http://wiki.ecmascript.org/doku.php?id=harmony:egal). + if (a === b) return a !== 0 || 1 / a === 1 / b; var typeA = typeof a; if( typeA != typeof b ) return false; if( typeA == 'object' ) return a && b ? eq( a, b ) : a === b; From ebc7beaf47d5ad16c8bf63b200de6fb5dd9b7aca Mon Sep 17 00:00:00 2001 From: Vlad Balin Date: Sun, 17 May 2015 00:51:43 +0300 Subject: [PATCH 3/3] optimized NaN case --- underscore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/underscore.js b/underscore.js index 6d8bb7164..d36e07552 100644 --- a/underscore.js +++ b/underscore.js @@ -1204,7 +1204,7 @@ var typeA = typeof a; if( typeA != typeof b ) return false; if( typeA == 'object' ) return a && b ? eq( a, b ) : a === b; - return a === 0 || a != a ? eq(a, b) : a === b; + return a != a ? b != b : a === b; }; // Is a given array, string, or object empty?