According to the documentation, there are 2 ways to get how many elements are inside the ElementArrayFinder (the result of element.all() call):
$$(".myclass").length, documented here:...the array has
lengthequal to thelengthof the elements found by theElementArrayFinderand each result represents the result of performing the action on the element.
$$(".myclass").count(), documented here:Count the number of elements represented by the
ElementArrayFinder.
What is the difference between these two methods and which one should be preferred?
$$(".myclass").lengthNeed to resolve the promise to get the length of element correctly.
// WORK
$$(".myclass").then(function(items){
items.length;
});
// DOES NOT WORK
$$(".myclass").length;
$$(".myclass").count()A wrapper for $$('.myclass').length which being a promise itself and doesn't require to resolve promise like .length
$$(".myclass").count();
which one should be preferred?
Unless there some complex business when locating $$(".myclass") and .then(function(items){...}) involved then items.length will give better performance.
Otherwise $$(".myclass").count() should always be used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With