Is "first item is not empty" faster than "count > 0"?

ERRATTA DEPARTMENT:

I see that someone just liked this old post reply of mine (Is "first item is not empty" faster than "count > 0"? - #2 by keith). Regardless of what I said at that time, the proper way to assess a Bubble list’s emptiness is if the lists's :count is < 1. The basic theorizing that might be present in this reply is wrong and any observations I made at that time were likely inaccurate or anecdotal.

Having much more experience with Bubble internals since that time, I can confidently tell you that (1) a Bubble Search can return its :count in no time at all (in fact, the result of a Search is an object that contains simply a function that can be used to retrieve the individual search results themselves, and (2) that response includes a method (.length()) that returns the number of search results contained therein). While the .length() method is a function, it doesn’t actually seem to make a call to the database, the resultant value seems to just travel along with the Search result. So…

This itself would tell us that the :count property is faster/superior to evaluating “first item is not empty” (which won’t always give us the correct answer anyways as it is possible for a Bubble list to have null/empty values and one of these might be in the first position).

Regardless, the results of the Search action are always an object that includes the .length() method and it’s not actually possible that “first item is not empty” would be faster than assessing the :count. So, the right (and fastest) way to do this is by checking :count. I’d forgotten about all of this discussion from back in the day. Thankfully, most current advice and examples use comparisons of :count to assess the “emptiness” of a list (that is, a list has no items if count is less than zero… if the count is greater than zero, the list has items… and the count parameter is provided as quickly as possible and cannot be slower than assessing whether the :first item is empty, which isn’t even the right thing to check anyway).

Edit: Further elucidation: All JavaScript arrays have a .length parameter which tells us how many elements the array contains. The Bubble List’s .length() method is the same thing, just formatted as a function/method.

6 Likes