Skip to content

Very slow on large JSONs #1

Closed
Closed
@antonioribeiro

Description

@antonioribeiro

Benchmarked it on a fairly large JSON and this is what I got:

json_decode: 439 records loaded in 5ms
json5_decode: 439 records loaded in 658238ms

Here's the code I'm using to test it:

require '/Users/antoniocarlos/code/pragmarx/pragmarx.com/vendor/pragmarx/countries/vendor/autoload.php';

$contents = file_get_contents('/Users/antoniocarlos/code/pragmarx/pragmarx.com/test.json');

show_diff(function () use ($contents) {
    return json_decode($contents, true);
});

show_diff(function () use ($contents) {
    return json5_decode($contents, true);
});

///-----------------------------------------------------------------

function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());

    return ((float) $usec + (float)$sec);
}

function show_diff($runner) {
    $start = microtime_float();

    $count = count($runner());

    $time = (int) ((microtime_float() - $start) * 1000);

    echo "$count records loaded in {$time}ms\n";
}

And the JSON file was generated using JSON generator:

[
  '{{repeat(1500, 7)}}',
  {
    _id: '{{objectId()}}',
    index: '{{index()}}',
    guid: '{{guid()}}',
    isActive: '{{bool()}}',
    balance: '{{floating(1000, 4000, 2, "$0,0.00")}}',
    picture: 'http://placehold.it/32x32',
    age: '{{integer(20, 40)}}',
    eyeColor: '{{random("blue", "brown", "green")}}',
    name: '{{firstName()}} {{surname()}}',
    gender: '{{gender()}}',
    company: '{{company().toUpperCase()}}',
    email: '{{email()}}',
    phone: '+1 {{phone()}}',
    address: '{{integer(100, 999)}} {{street()}}, {{city()}}, {{state()}}, {{integer(100, 10000)}}',
    about: '{{lorem(1, "paragraphs")}}',
    registered: '{{date(new Date(2014, 0, 1), new Date(), "YYYY-MM-ddThh:mm:ss Z")}}',
    latitude: '{{floating(-90.000001, 90)}}',
    longitude: '{{floating(-180.000001, 180)}}',
    tags: [
      '{{repeat(7)}}',
      '{{lorem(1, "words")}}'
    ],
    friends: [
      '{{repeat(3)}}',
      {
        id: '{{index()}}',
        name: '{{firstName()}} {{surname()}}'
      }
    ],
    greeting: function (tags) {
      return 'Hello, ' + this.name + '! You have ' + tags.integer(1, 10) + ' unread messages.';
    },
    favoriteFruit: function (tags) {
      var fruits = ['apple', 'banana', 'strawberry'];
      return fruits[tags.integer(0, fruits.length - 1)];
    }
  }
]

Environment

  • colinodell/json5 v1.0.1
  • PHP 7.2.0 (cli) (built: Dec 3 2017 21:46:44) ( NTS )
  • macOS 10.13.1 (17B1003)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions