Skip to content

Instantly share code, notes, and snippets.

@alexpott
Created August 20, 2015 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexpott/8b3d9b3a55e87919ec20 to your computer and use it in GitHub Desktop.
Save alexpott/8b3d9b3a55e87919ec20 to your computer and use it in GitHub Desktop.
<?php
use Drupal\Component\Utility\Html;
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
// Boot the environment.
$autoloader = require_once 'autoload.php';
$request = Request::createFromGlobals();
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel->boot();
$kernel->preHandle($request);
// Create test values.
$arr = array_fill(0,20, 'lorem ipsum');
$twig_render_array = ['#theme' => 'inline_list', '#items' => $arr];
$markup_render_array = ['#separator' => ', ', '#markup' => $arr];
// Do an unrelated render to set up stuff...
$renderer = \Drupal::service('renderer');
$markup = ['#markup' => 'ignore'];
$renderer->renderPlain($markup);
// Test #markup.
$time_start = microtime(true);
for ($i=0;$i<10;$i++) {
$renderer->renderPlain($markup_render_array);
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "#markup render array in $time seconds\n";
// Test twig template.
$time_start = microtime(true);
for ($i=0;$i<10;$i++) {
$renderer->renderPlain($twig_render_array);
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Twig render array in $time seconds\n";
// Test implode().
$time_start = microtime(true);
for ($i=0;$i<10;$i++) {
foreach ($arr as $value) {
Html::escape($value);
}
implode(', ', $arr);
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Implode + Escape in $time seconds\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment