Skip to content

Commit

Permalink
Merge pull request jashkenas#515 from octatone/master
Browse files Browse the repository at this point in the history
Proposed solution for _.size speed improvement RE issue jashkenas#496
  • Loading branch information
jashkenas committed Mar 20, 2012
2 parents 33be5c6 + 4c2a85f commit 594f1e2
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
1 change: 1 addition & 0 deletions test/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ $(document).ready(function() {

test('collections: size', function() {
equal(_.size({one : 1, two : 2, three : 3}), 3, 'can compute the size of an object');
equal(_.size([1, 2, 3]), 3, 'can compute the size of an array');
});

});
2 changes: 1 addition & 1 deletion underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@

// Return the number of elements in an object.
_.size = function(obj) {
return _.toArray(obj).length;
return _.isArray(obj) ? obj.length : _.keys(obj).length;
};

// Array Functions
Expand Down

0 comments on commit 594f1e2

Please sign in to comment.