Skip to content

HTML insertion / format: html performance issue #234

@cxj

Description

@cxj

What's going on here, that causes such a large performance hit when inserting HTML into a template?

<?php
require "vendor/autoload.php";

// 1-kilobyte string of random characters:
$oneK = random_bytes(1024);

// 1-kilobyte string of HTML:
$html = <<<HTML
<tr><td>one </td><td>two</td><td>three</td><td>four</td><td>five</td><td>six</td><td>seven</td></tr>
<tr><td>one </td><td>two</td><td>three</td><td>four</td><td>five</td><td>six</td><td>seven</td></tr>
<tr><td>one </td><td>two</td><td>three</td><td>four</td><td>five</td><td>six</td><td>seven</td></tr>
<tr><td>one </td><td>two</td><td>three</td><td>four</td><td>five</td><td>six</td><td>seven</td></tr>
<tr><td>one </td><td>two</td><td>three</td><td>four</td><td>five</td><td>six</td><td>seven</td></tr>
<tr><td>one </td><td>two</td><td>three</td><td>four</td><td>five</td><td>six</td><td>seven</td></tr>
<tr><td>one </td><td>two</td><td>three</td><td>four</td><td>five</td><td>six</td><td>seven</td></tr>
<tr><td>one </td><td>two</td><td>three</td><td>four</td><td>five</td><td>six</td><td>seven</td></tr>
<tr><td>one </td><td>two</td><td>three</td><td>four</td><td>five</td><td>six</td><td>seven</td></tr>
<tr><td>one </td><td>two</td><td>three</td><td>four</td><td>five</td><td>six</td><td>seven</td></tr>
HTML;

$xml = ' <main> @@ </main> ';

$tss = <<<TSS
main {
    content: data(html);
    content-mode: replace;
    format: html;
}
TSS;

$tss2 = <<<TSS2
main {
    content: data(html);
    content-mode: replace;
}
TSS2;

$data['html']  = str_repeat($html, 4000);
$data2['html'] = str_repeat($oneK, 4000);

test($xml, $tss,  $data2, "format html with random string");
test($xml, $tss2, $data2, "no format with random string");

test($xml, $tss,  $data, "format html with HTML");
test($xml, $tss2, $data, "no format with HTML");

function test($xml, $tss, $data, $title) {
    $template = new Transphporm\Builder($xml, $tss);

    $start = microtime(true);
    $template->output($data)->body;
    $stop = microtime(true);

    echo sprintf("%30s time: %12.6f seconds\n", $title, ($stop - $start));
}

And the output on my laptop:

format html with random string time:     0.033743 seconds
  no format with random string time:     0.002971 seconds
         format html with HTML time:    36.562475 seconds
           no format with HTML time:     0.123278 seconds

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions