Home PHP Arrays vs Objects
Arrays vs Objects PDF Print E-mail
Written by Nate Lyman   
Saturday, 20 February 2010 07:50

This post isn't a stab at OOP at all.  This post is to simply show the differences in performance between Arrays and Objects.  For most applications it probably doesn't make a huge difference but if you're working on a large web application or backend job microseconds count.

I will use a snippet of code that does exactly the same thing but store the data in an object and an array.  First lets look at using an object.

Show/Hidden php code

Now, lets examine the difference if you use an array with string keys.

Show/Hidden php code

As you can tell, the array with string kess performs worse than an object.   Lets examine using a numeric key.

Show/Hidden php code

Look at that, nearly twice as fast as both objects and arrays with string indexes.  For the seasoned programmer this comes as no surprise, but for a more junior programmer this is something to keep in mind.  Yes, an assiciative array makes for cleaner code, but can have a pretty big impact on performance at scale.

 

Add comment


Security code
Refresh