Skip to content

Commit

Permalink
Optimize _.isEqual for primatives
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed May 17, 2015
1 parent 4e2d94d commit 7f59e40
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,7 @@

// Perform a deep comparison to check if two objects are equal.
_.isEqual = function(a, b) {
return eq(a, b);
return a === a && a !== 0 && typeof a !== 'object' && typeof b !== 'object' ? a === b : eq(a, b);
};

// Is a given array, string, or object empty?
Expand Down

1 comment on commit 7f59e40

@jdalton
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jridgewell

... without another function or duplicating all the checks already in eq()

Without another function yap, but it's a dup check.

@gaperton @megawac
The simple object check won't due in older enviros that report regexes as type of function. It means in those environments _.isEqual(/a/, /a/) will return false with these optimizations.

Also the benchmark is looking at millions of ops/sec vs. more millions of ops/sec so this is not likely a bottleneck (a micro-opt).

Please sign in to comment.