Permalink
Browse files

Optimized minify wrapper function

Previously a single instance of the Minifier was reused between
requests, but this update forces a new one to be created each time.
This actually improves performance, especially in cases where there are
a lot of small scripts being processed, as the cleanup is more
efficient and less checks have to be done to make sure the instance was
cleanly reset.
  • Loading branch information...
1 parent f70f09e commit 08001d16f03f6be3da15d96035934a295cc526f0 @tedivm committed Jan 26, 2014
Showing with 11 additions and 9 deletions.
  1. +11 −9 src/JShrink/Minifier.php
View
@@ -101,18 +101,23 @@ public static function minify($js, $options = array())
ob_start();
$currentOptions = array_merge(self::$defaultOptions, $options);
- if(!isset(self::$jshrink))
- self::$jshrink = new Minifier();
-
- self::$jshrink->breakdownScript($js, $currentOptions);
+ $jshrink = new Minifier();
+ $jshrink->breakdownScript($js, $currentOptions);
+ unset($jshrink);
// Sometimes there's a leading new line, so we trim that out here.
return ltrim(ob_get_clean());
} catch (Exception $e) {
- if(isset(self::$jshrink))
- self::$jshrink->clean();
+ if(isset($jshrink)){
+ // Since the breakdownScript function probably wasn't finished
+ // we clean it out before discarding it.
+ $jshrink->clean();
+ unset($jshrink);
+ }
+
+ // without this call things get weird, with partially outputted js.
ob_end_clean();
throw $e;
}
@@ -127,9 +132,6 @@ public static function minify($js, $options = array())
*/
protected function breakdownScript($js, $currentOptions)
{
- // reset work attributes in case this isn't the first run.
- $this->clean();
-
$this->options = $currentOptions;
$js = str_replace("\r\n", "\n", $js);

0 comments on commit 08001d1

Please sign in to comment.