There are 1805 elements in this array. I am looping through each one of them and outputting the ID and destroying the follower.
How do I find the big O notation here?
Would the better and quicker option be to use followers.destroy_all?
followers.each { |f| puts f.id; f.destroy! };
CodePudding user response:
When you look at the source code of destroy_all
then you will notice that it basically does the same as your example. It iterates over all records in the relation and calls destroy
on each of them. I expect it still to be a bit faster than your version because calling puts
doesn't come for free. But both versions are O(n)
If possible you might want to use delete_all
instead which doesn't load all records into memory and doesn't call destroy
on every single element. Instead, it deletes all records in the relation with one single SQL query which makes this method way faster than destroy_all
. But of course delete_all
doesn't trigger callbacks and therefore might not work for you.