Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use an associative array or an object?

As we all know, json_decode gives you the option of returning an associative array or an object. There are many other situations where we have the two options as well. Obviously in some cases using one or the other is more "appropriate" based on the type of data you're dealing with (a group of data pertaining to one item vs. a list of items).

What I'm wondering is, is there any difference in efficiency in using one vs. the other, in terms of memory, speed, etc? I'm particularly interested in access time for a very large object - how does that compare to a very large array, and why?

Sorry if this has been discussed before, search turned up nothing. I'm writing a benchmark, which may tell me which one is better, but won't help me understand why.

like image 317
andrewtweber Avatar asked Dec 14 '11 00:12

andrewtweber


People also ask

Should I use object or array?

Think about what your particular data represents: If it's a single entity with named properties, you want an object. If it's a group of entities of the same type/shape, or if order matters, you likely want an array.

Is an associative array an object?

An associative array is an array with string keys rather than numeric keys. Associative arrays are dynamic objects that the user redefines as needed. When you assign values ​​to keys in a variable of type Array, the array is transformed into an object, and it loses the attributes and methods of Array.

What is the difference between associative array and object in JavaScript?

Associated array is defined as key-value pairs which is expressed as the object in JavaScript. The outer object assigned to check has a key pattern and an value of another object. The inner object has keys of name , email ... and corresponding values of regular expression objects.

Which is faster array or object in PHP?

Conclusion for php 5.5. Class is slower than Arrays thanks to the optimization of PHP 5.5 and arrays. stdClass is evil. Class still uses less memory than Arrays.


2 Answers

Many programmers prefer using true as the second argument to json_decode since the returned assoc array will be very similar to how you handle objects in javascript.

Returning a proper object will require reading about how such is used and what not, and since most programmers are well familiar with associative arrays that's more preferrable, especially if the code will be maintained by a team of developers. Code should be easily understandable.

Regarding questions about performance I don't think you'll need to worry about that since the bottle neck in most (all) cases will be elsewhere. Unless you are parsing a massive string, and by that I mean really huge, you shouldn't need to make any benchmarks. I believe the difference between returning an assoc array vs a proper object will be minor.


Performance Benchmark (parsing)

I found a rather large json string here and made some adjustments to make it even bigger, final size is 84 578 bytes.

I then parsed the string using both alternatives (associative array vs object) 1 000 times each, and I ran the test three times. The results are given below:

1st run

  JSON object exec: 4.06122 s   JSON assoc  exec: 3.28679 s ------------------------------------- assoc is faster by 19.07% 

2nd run

  JSON object exec: 4.09614 s   JSON assoc  exec: 3.29216 s ------------------------------------- assoc is faster by 19.63% 

3rd run

  JSON object exec: 4.08762 s   JSON assoc  exec: 3.29960 s ------------------------------------- assoc is faster by 19.28% 

Performance Benchmark (read/write)

This benchmark is to show which one of stdObject and Array() is faster, I'm using a parsed modified json file (bigger one) than in the previous benchmark.

Each read/write test was run 100 000 times (ie. the code given below was executed that many times).

json_decode ($json_data)

for ($i =0; $i < 24; ++$i){   $a = $object[$i]->user->profile_sidebar_border_color . "stackoverflow";   $object[$i]->nested->entities->user_mentions[0]->indices[$i&1] += 1; } 

json_decode ($json_data, true)

for ($i =0; $i < 24; ++$i){   $a = $assoc[$i]['user']['profile_sidebar_border_color'] . "stackoverflow";   $assoc[$i]['nested']['entities']['user_mentions'][0]['indices'][$i&1] += 1; } 

1st run

  JSON object read/write: 3.05421 s   JSON assoc  read/write: 2.51932 s ------------------------------------- assoc is faster by 17.51% 

2nd run

  JSON object read/write: 3.06307 s   JSON assoc  read/write: 2.52701 s ------------------------------------- assoc is faster by 17.50% 

3rd run

  JSON object read/write: 3.06109 s   JSON assoc  read/write: 2.52248 s ------------------------------------- assoc is faster by 17.60% 

PHP version

PHP 5.3.6 (cli) (built: Aug 13 2011 19:04:57) Copyright (c) 1997-2011

The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend

Technologies

like image 111
Filip Roséen - refp Avatar answered Oct 12 '22 00:10

Filip Roséen - refp


Performance Benchmark (access time)

Here is my benchmark. I was mostly interested in access time. I populated an array with 10,000 variables, cast it as an object, then for both the object and array I simply accessed one of the variables 10,000 times. Part of the code:

$arr = array(); for( $i=0; $i<10000; $i++ ) {     $arr['test'.$i] = 'Hello. My name is Inigo Montoya. You killed my father. Prepare to die.'; } $obj = (object)$arr;  $tests = array(0,1000,2000,3000,4000,5000,6000,7000,8000,9999);  foreach( $tests as $test ) {     $test_name = 'test'.$test;      $start = microtime(true);     for( $i=0; $i<10000; $i++ ) {         $var = $obj->$test_name;     }     $end = microtime(true);     $elapsed = $end - $start;      $start = microtime(true);     for( $i=0; $i<10000; $i++ ) {         $var = $arr[$test_name];     }     $end = microtime(true);     $elapsed = $end - $start; } 

Results

I ran the test multiple times; here is one of the typical result sets; times are in milliseconds.

            Object    Array ------------------------------ test0       4.4880    4.1411 test1000    4.5588    4.2078 test2000    4.5812    4.2109 test3000    4.5240    4.2000 test4000    4.5800    4.2648 test5000    4.5929    4.2000 test6000    4.5311    4.2260 test7000    4.6101    4.2901 test8000    4.5331    4.1370 test9999    4.5100    4.1430 

The array was an average of 8.3% faster than the object (7.7% in the set above). The index of the variable we are trying to access has no effect on the access time.

Seeing the comments above I'm embarrassed to say I'm on PHP 5.3.4.

like image 33
andrewtweber Avatar answered Oct 12 '22 00:10

andrewtweber