4

I have some json_encode related issues : i need to use a big array (several 100k items), each with very simple structure (one key, one string value). json_decode works ok, but as soon as i want to json_encode it, it's awfully slow. Since i fully control the data here, i tried to write a super simple json encoder, and it's fast. I'm quite surprised, since my encoding function is crude, and oes not have any of the inner php optimizations that are quite certainly present in json_encode.

Any idea what the problem might be ?

I put my encoder function below for reference.

Thanks

protected function simpleJsonEncoder($data) {
    if (is_array($data)) {
        $is_indexed = (array_values($data) === $data);
        $tab_str = [];
        if ($is_indexed) {
            foreach($data as $item) {
                $str_item = $this->simpleJsonEncoder($item);
                $tab_str[] = $str_item;
            }
            $result = '[' . implode(',', $tab_str) . ']';
        }
        else {
            foreach($data as $index => $item) {
                $str_item = $this->simpleJsonEncoder($item);
                $tab_str[] = '"' . htmlspecialchars($index, ENT_QUOTES) . '":' . $str_item;
            }
            $result = '{' . implode(',', $tab_str) . '}';
        }
    }
    else {
        $result = '"' . htmlspecialchars($data, ENT_QUOTES) . '"';
    }

    return $result;
}
MarvinLeRouge
  • 444
  • 5
  • 15
  • I don't understand your comment : the loops you're seeing here are part of a function created BEACAUSE json_encode was super slow. There is no loop is the original code with json_encode : `$json = json_encode($big_tab);` – MarvinLeRouge Jun 11 '18 at 12:00
  • Can't find it. Do you have the link ? – MarvinLeRouge Jun 12 '18 at 08:43
  • Well, it discusses the pros and cons of json vs serialize, but no real clue as to why json_encode is so slow with big simple array. I really don't understand how my function can be faster than native php, seems absurd to me. – MarvinLeRouge Jun 12 '18 at 09:00

1 Answers1

0

For posterity: I've been trying to find alternatives to json_encode for syncing large amounts of data, serialize is way quicker but returns a string much larger in size obviously. I stumbled across this page. I tried out this function - the md5 hash is different from json_encode and the time difference is negligible. From everything I've read recently, they've optimized json_encode somewhere along the line.

I'm on PHP 7.3 and time is in seconds (big object)

"user_func_hash": "xxx",
"user_func_time": 45.33081293106079,
"json_encode_hash": "yyy",
"json_encode_time": 45.759231090545654
Mitch
  • 51
  • 2