PHP usort is slow slow ssslow…..

PHP’s native usort array sorting function is handy especially when you need to sort an array by some custom criteria, and other sorting options like sort, asort or ksort just don’t have enough flexibility.

But man, on any reasonably sized dataset, usort is just too slow! How slow you ask? E.g. with an array of 150 elements, using usort adds 3 seconds to loading time on a webpage that displays this data (versus no sorting at all). 3 seconds might not seem like much, but consider:

  • Most nowadays web users are accustomed to pages loading in 1-3 seconds total, otherwise they simply click back and don’t bother waiting. So using up 3 seconds just for a sorting function is not really acceptable
  • That 3 second lag for usort to do its thing gets exponentially larger as the dataset grows

Is there a solution? Only thing I can think of is code refactor to not require usort. Sorry but that’s the sad truth.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s