Skip to content

Instantly share code, notes, and snippets.

@ConnerAiken
Created October 11, 2019 18:18
Show Gist options
  • Save ConnerAiken/241df2af1b88661bee3bfe6a647d6a52 to your computer and use it in GitHub Desktop.
Save ConnerAiken/241df2af1b88661bee3bfe6a647d6a52 to your computer and use it in GitHub Desktop.
PHP 7 Benchmark test, regex for empty string validation vs php functions
<?php
// This script compares regex validation vs php validation speeds for a sample JSON data set
// =================================
// Fetch some sample data
// =-=-=-=-=-=-=-=-=-=-=-=-
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'https://jsonplaceholder.typicode.com/photos');
$result = curl_exec($ch);
curl_close($ch);
$objs = json_decode($result);
$objsRegex = json_decode($result);
// =================================
// validate using php
// =-=-=-=-=-=-=-=-=-=-=-=-
// See https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/
function isEmpty($val) {
if(is_string($val) && $val !== "0") {
return empty(trim($val));
}else {
return false;
}
}
// Mock some bad data
$objs[3]->id = 0;
$objs[3]->title = " ";
$objsRegex[3]->id = 0;
$objsRegex[3]->title = " ";
$start = microtime(true);
// ==========================================
// Try Approach 1 (PHP Function Scrubbing)
// ================================
// Loop the json objects (5000 records)
for ($i = 0; $i < count($objs); $i++) {
// Convert from Obj to Array for better performance
$obj = (array) $objs[$i];
// Get an array of keys
$keys = array_keys($obj);
for($x = 0; $x < count($keys); $x++) {
$obj[$keys[$x]] = isEmpty($obj[$keys[$x]]) ? null : $obj[$keys[$x]];
}
}
echo "PHP Loop took: " . (microtime(true) - $start) . " microseconds" . PHP_EOL;
// ==========================================
// Try Approach 2 (PHP Regex Scrubbing)
// ================================
$start = microtime(true);
// If we re-encode
//$jsonPayload = json_encode($objsRegex);
//$jsonPayload = preg_replace('/\"\s+\"/', '""', $jsonPayload);
//$jsonPayload = preg_replace('/\"\"/', 'null', $jsonPayload);
// If we do not re-encode
$result = preg_replace('/\"\s+\"/', '""', $result);
$result = preg_replace('/\"\"/', 'null', $result);
echo "Regex took: " . (microtime(true) - $start) . " microseconds";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment