Should I use an associative array or an object?

后端 未结 3 554
别跟我提以往
别跟我提以往 2021-02-01 16:23

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 we

3条回答
  •  孤城傲影
    2021-02-01 17:18

    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

提交回复
热议问题