PHP Classes

File: tests/benchmarks/average-case.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   iaso PHP JSON Parser Library   tests/benchmarks/average-case.php   Download  
File: tests/benchmarks/average-case.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: iaso PHP JSON Parser Library
Parse JSON strings immune to hash-DoS attacks
Author: By
Last change:
Date: 2 years ago
Size: 1,302 bytes
 

Contents

Class file image Download
<?php
use ParagonIE\Iaso\JSON;

require
'../../vendor/autoload.php';

JSON::parse('[]');

$string = '{
  "data": [{
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "JSON API paints my bikeshed!",
      "body": "The shortest article. Ever.",
      "created": "2015-05-22T14:56:29.000Z",
      "updated": "2015-05-22T14:56:28.000Z"
    },
    "relationships": {
      "author": {
        "data": {"id": "41", "type": "people"},
        "data": {"id": "42", "type": "people"}
      }
    }
  }],
  "included": [
    {
      "type": "people",
      "id": "41",
      "attributes": {
        "name": "Jane",
        "age": 30,
        "gender": "genderfluid"
      }
    },
    {
      "type": "people",
      "id": "42",
      "attributes": {
        "name": "John",
        "age": 80,
        "gender": "male"
      }
    }
  ]
}'
;

$i = 0;
$start = $end = 0.000;

$start = \microtime(true);
for (
$i = 0; $i < 1000; ++$i) {
   
$native = \json_decode($string);
}
$end = \microtime(true);

$diff = $end - $start;
echo
number_format($diff, 3) . ' seconds (native)', PHP_EOL;

$start = \microtime(true);
for (
$i = 0; $i < 1000; ++$i) {
   
$native = JSON::parse($string);
}
$end = \microtime(true);

$diff = $end - $start;
echo
number_format($diff, 3) . ' seconds (Iaso)', PHP_EOL;