YAML or JSON | Drupal 8

YAML or JSON

Submitted by editor on Thu, 05/11/2017 - 13:42
Question

Is it preferable to use YAML or JSON on drupal 8 ? What are the differents ?

Yml vs Json

A make a small test to test YAML vs PHP performance. The result is:

  YAML JSON
Encode 30K, x100 3s 0s
Encode 30K, x1000 27s 1s
Decode  30K, x100 14s 0s
Decode  30K, x1000 142s 2s
File Size 30K 21K

   

I think no need to explain the result !!!

The Test:
$file = 'views.view.files.yml';//30KB
$file_yml = file_get_contents($file);
$file_json = file_get_contents($file . '.json');
//Prepare
$data = \Drupal\Component\Serialization\Yaml::decode($file_yml);
//file_put_contents($file . '.json', \Drupal\Component\Serialization\Json::encode($data));
$loops = 1000;
$time = time();
for ($i = 0; $i < $loops; $i++) {
//Decode test
//$temp = \Drupal\Component\Serialization\Yaml::decode($file_yml);
//$temp = \Drupal\Component\Serialization\Json::decode($file_json);
//Encode test
//$temp = \Drupal\Component\Serialization\Yaml::encode($data);
//$temp = \Drupal\Component\Serialization\Json::encode($data);
//Check
//print_r($temp); break;
}
$time = time() - $time;
echo "Time : $time sec\n";

Nota : On Yaml (.yml) you can appen data without reading (You can't do with JSON). Example:

$yml = Yaml::encode([$yml_data]);
file_put_contents('file_name.yml', $yml, FILE_APPEND);

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.