diff --git a/test/collections.js b/test/collections.js index 71ef5a71c..18b242769 100644 --- a/test/collections.js +++ b/test/collections.js @@ -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'); }); }); diff --git a/underscore.js b/underscore.js index 21a45c0d9..c699c991b 100644 --- a/underscore.js +++ b/underscore.js @@ -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