Is there any difference between $(\".selector\").size()
and $(\".selector\").length
?
.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.
Yes! There is now a very significant difference. .size() is deprecated. Always use .length
instead.
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.
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..
Length is much faster.
See the tutorial size vs. length.
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.