I often work with junior programmers and somehow they tend to use syntax like this:
return unless array_data.any? # or do something if array_data.any?
And there’s almost nothing wrong with it, as long as you don’t work with huge arrays.
Here’s a difference in speed between empty?, blank? and any? with different array sizes (worse scenario):
Why any? is so slow with bigger arrays? Well, basically because it iterates through the whole the collection and checks if there’s at least one non-false and non-nil element (when no block given). Of course it will stop with the first non-false value, but we were pessimistic and we assumed that there are no elements like that. Many developers assume that any? answers the: “is there anything in the array” question, when it really answers: “is there at least one non-false and non-nil element in the array”. And this can make huge difference with bigger arrays.
Of course if we decide to have a true-like value in a random place of an array, things look a bit different:
Conclusion: Using any? to determine, if there’s anything in the array can have a huge impact on your code performance if you work with bigger arrays. And even if you don’t, having bad habits is not a good idea.