0

I have this code in my init file, to make my html size smaller for visitors and this helps me when reading the html code.

function sanitize_output($buffer) {

    $search = array(
        '/\>[^\S ]+/s',
        '/[^\S ]+\</s',
        '/(\s)+/s'
        );
    $replace = array(
        '>',
        '<',
        '\\1'
        );

    $blocks = preg_split('/(<\/?pre[^>]*>)/', $buffer, null, PREG_SPLIT_DELIM_CAPTURE);
    $buffer = '';

    foreach($blocks as $i => $block)
    {
      if($i % 4 == 2)
        $buffer .= $block;
      else 
        $buffer .= preg_replace($search, $replace, $block);
    }

    return $buffer;
}

ob_start("sanitize_output");

Problem is that the page load time increased from (.9s - 1.2s) to (1.9s - 2.6s). And I read the answer here https://stackoverflow.com/a/9943436/7059329, stating that ob_start() will affect the page load times. How can I make the load time faster? Should I remove the ob_start() code?

Community
  • 1
  • 1
Dumb Question
  • 365
  • 1
  • 3
  • 14
  • 2
    The regex is the preformance issue here, not `ob_start`. – JustOnUnderMillions Jan 16 '17 at 07:26
  • Is there a way for me to solve this? Please tell me @JustOnUnderMillions. – Dumb Question Jan 16 '17 at 07:28
  • Benchmarking & Testing your regexes, maybe they can optimised. Maybe you can do it with just one `preg_replace` instead of using `preg_split` and a loop. Give it a try. – JustOnUnderMillions Jan 16 '17 at 07:31
  • `ob_start()` will output the page after your script has finished execution. The idea is that you can do further checks on a rendered page before sending to browser (like what you have done). You need to test and make sure that this is causing the issue. Remove the above code from your file and see how responsive if it is. If your still seeing the same response times, then follow the advice above. – Kitson88 Jan 16 '17 at 07:34
  • I dont really get the replace stuff full. But i dont thing you have to remove spaces before and after a html-node, because most of the browser remove them automaticly. – JustOnUnderMillions Jan 16 '17 at 07:35
  • I changed it now, first, in my init file i only put the `ob_start()` after that, I follow the answer here by Svish http://stackoverflow.com/a/5258778/7059329. Page load time lessen by .4s. – Dumb Question Jan 16 '17 at 10:26

0 Answers0