Count, Size & Length methods. Which one to use where? What’s the difference between these?

Even the experienced developers will struggle to answer this correctly. Where to use count method? Where to use size method? Where to use length method? What is the difference among these? What is the advantages/disadvantages of using this? Which one is effective in using which areas? etc…, Find the answer below for the above questions :-

Consider, you have use any of these methods in both Active record (and) as a ruby method. First let us see which one to use as a ruby method.

AS A RUBY METHOD

names=[“Sachin”, “Dravid”, “Ashwin”]

Now, if you use names.size it will return the value 3. If you use names.length again it will return the same value 3. But I would recommended to go for names.length rather than names.size. Because, see the below example :-

If you try to find the size of any numbers using size method, it will return you some value whereas length method will throw the error. So if we use size method, we will not be 100% sure whether the output returned is correct or not..

1.size => 4

1.length => NoMethodError: undefined method `length’ for 1:Fixnum

AS A ACTIVE RECORD METHOD

Assume that we have 2 tables, one is user  and another is chats which has the relation as below :-

User -> has many –> Chats

Chats -> belongs_to –> User

user.chats.length –> This always loads the content of the association into memory, and then returns the number of elements loaded.

Chat Load (0.1ms)   SELECT * FROM `chats` WHERE (`chats`.user_id = 1) 

user.chats.count –> Determines the number of elements with the simple SQL count query.

1st time (Before the collection was loaded)

Chat Columns (1.0ms) SHOW FIELDS FROM `chats`
SQL (0.1ms) SELECT count(*) AS count_all FROM `chats` WHERE (`chats`.user_id = 1)

2nd time (Before the collection was loaded)

SQL (0.1ms) SELECT count(*) AS count_all FROM `chats` WHERE (`chats`.user_id = 1)

After running user.chats.length (or collection was already loaded)

SQL (0.1ms) SELECT count(*) AS count_all FROM `chats` WHERE (`chats`.user_id = 1)

user.chats.size –> This method has some caching mechanism, and it is like using the ‘count’ method if the collection was not already loaded (Executes the SQL count query). But if the collection was already loaded, it just takes the caching mechanism to display the size without even firing a SQL query.

1st time (Before the collection was loaded)

Chat Columns (1.0ms) SHOW FIELDS FROM `chats`

SQL (0.1ms) SELECT count(*) AS count_all FROM `chats` WHERE (`chats`.user_id = 1)

2nd time (Before the collection was loaded)

SQL (0.1 ms) SELECT count(*) AS count_all FROM `chats` WHERE (`chats`.user_id = 1)

After running user.chats.length (or collection was already loaded)

(NO SQL QUERY WILL BE FIRED, AND IT RETURNS THE CACHE VALUE)

CONCLUSION 


If you are going to use any of these methods during ActiveRecord I would suggest you to use the SIZE method.

If you are going to use any of these methods during normal ruby process I would suggest you to use the LENGTH method.