Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Many programmers prefer using <code>true</code> as the second argument to <a href="http://se.php.net/manual/en/function.json-decode.php">json_decode</a> since the returned <em>assoc array</em> will be very similar to how you handle objects in javascript.</p> <p>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.</p> <p>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.</p> <hr> <h2><strong>Performance Benchmark (parsing)</strong></h2> <p>I found a rather large json string <a href="https://raw.githubusercontent.com/jmhodges/jsonpp/master/data/tweet.json">here</a> and made some adjustments to make it even bigger, final size is 84 578 bytes.</p> <p>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:</p> <p><em>1st run</em></p> <pre><code> JSON object exec: 4.06122 s JSON assoc exec: 3.28679 s ------------------------------------- assoc is faster by 19.07% </code></pre> <p><em>2nd run</em></p> <pre><code> JSON object exec: 4.09614 s JSON assoc exec: 3.29216 s ------------------------------------- assoc is faster by 19.63% </code></pre> <p><em>3rd run</em></p> <pre><code> JSON object exec: 4.08762 s JSON assoc exec: 3.29960 s ------------------------------------- assoc is faster by 19.28% </code></pre> <hr> <h2><strong>Performance Benchmark (read/write)</strong></h2> <p>This benchmark is to show which one of <code>stdObject</code> and <code>Array()</code> is faster, I'm using a parsed modified json file (bigger one) than in the previous benchmark.</p> <p>Each read/write test was run 100 000 times (ie. the code given below was executed that many times).</p> <p><strong>json_decode ($json_data)</strong></p> <pre><code>for ($i =0; $i &lt; 24; ++$i){ $a = $object[$i]-&gt;user-&gt;profile_sidebar_border_color . "stackoverflow"; $object[$i]-&gt;nested-&gt;entities-&gt;user_mentions[0]-&gt;indices[$i&amp;1] += 1; } </code></pre> <p><strong>json_decode ($json_data, true)</strong></p> <pre><code>for ($i =0; $i &lt; 24; ++$i){ $a = $assoc[$i]['user']['profile_sidebar_border_color'] . "stackoverflow"; $assoc[$i]['nested']['entities']['user_mentions'][0]['indices'][$i&amp;1] += 1; } </code></pre> <p><em>1st run</em></p> <pre><code> JSON object read/write: 3.05421 s JSON assoc read/write: 2.51932 s ------------------------------------- assoc is faster by 17.51% </code></pre> <p><em>2nd run</em></p> <pre><code> JSON object read/write: 3.06307 s JSON assoc read/write: 2.52701 s ------------------------------------- assoc is faster by 17.50% </code></pre> <p><em>3rd run</em></p> <pre><code> JSON object read/write: 3.06109 s JSON assoc read/write: 2.52248 s ------------------------------------- assoc is faster by 17.60% </code></pre> <hr> <p><strong>PHP version</strong></p> <blockquote> <p>PHP 5.3.6 (cli) (built: Aug 13 2011 19:04:57) Copyright (c) 1997-2011</p> <p>The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend</p> <p>Technologies</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload