Merge pull request #515 from octatone/master

Proposed solution for _.size speed improvement RE issue #496
This commit is contained in:
Jeremy Ashkenas
2012-03-20 08:16:19 -07:00
2 changed files with 2 additions and 1 deletions

View File

@@ -275,6 +275,7 @@ $(document).ready(function() {
test('collections: size', function() { test('collections: size', function() {
equal(_.size({one : 1, two : 2, three : 3}), 3, 'can compute the size of an object'); 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');
}); });
}); });

View File

@@ -309,7 +309,7 @@
// Return the number of elements in an object. // Return the number of elements in an object.
_.size = function(obj) { _.size = function(obj) {
return _.toArray(obj).length; return _.isArray(obj) ? obj.length : _.keys(obj).length;
}; };
// Array Functions // Array Functions