jQuery size() method vs length attribute

Jquery

Jquery Problem Overview


Is there any difference between $(".selector").size() and $(".selector").length ?

Jquery Solutions


Solution 1 - Jquery

No. size() returns length. By using length you only avoid one extra method call.

Solution 2 - Jquery

Length returns the same thing and is slightly faster according to the jQuery documentation.

Source: http://api.jquery.com/size/

Solution 3 - Jquery

They will both give you the same result but .length is slightly faster.

See <http://api.jquery.com/size/>;:

> The .length property is a slightly faster way to get this information.

Solution 4 - Jquery

Length is much faster.

See the tutorial size vs. length.

Solution 5 - Jquery

.size() is a Method call, which returns the length property. So you either call the method to return the property, or you retrieve the property directly.

The method (.size()) is probably the one you should be using, as it was most likely implemented to abstract away from the possibility of the length property being changed.

Solution 6 - Jquery

Yes! There is now a very significant difference. .size() is deprecated. Always use .length instead.

Solution 7 - Jquery

JQuery size() is a method & length is property and property is faster than method because size() internally calls length. so better to call length directly.

Solution 8 - Jquery

jQuery .size() and .length both return the number of elements in the jQuery object.

Size() and length in jQuery both returns the number of element in an object but length is faster than the size because length is a property and size is a method and length property does not have the overhead of a function call.

Solution 9 - Jquery

If you will read length property then only time required to access an object property will be needed.

However if you will call size() then first of all a function will be called, this function will read length property internally and then return that value to the caller.

You can clearly see that you are doing the same thing in both cases. But if you call the function then it will include time for calling a function + returning that value too..

Solution 10 - Jquery

Both are okay. However, length is better being a property than size() which is a method.

Moreso size() has been deprecated from jquery 1.8 and removed as of version 3.1.

So use length as much as possible

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjantimonView Question on Stackoverflow
Solution 1 - JqueryRoToRaView Answer on Stackoverflow
Solution 2 - JqueryFabianView Answer on Stackoverflow
Solution 3 - JqueryJon DeanView Answer on Stackoverflow
Solution 4 - JquerydoodlemoonchView Answer on Stackoverflow
Solution 5 - JqueryJaymzView Answer on Stackoverflow
Solution 6 - JqueryB RobsterView Answer on Stackoverflow
Solution 7 - JqueryRamesh kumarView Answer on Stackoverflow
Solution 8 - JquerySourav BasakView Answer on Stackoverflow
Solution 9 - JqueryVipul JethvaView Answer on Stackoverflow
Solution 10 - JqueryDavid AdeleView Answer on Stackoverflow