Skip to content

Commit

Permalink
Merge pull request #91 from ven-com/perf-fixes
Browse files Browse the repository at this point in the history
More performance tweaks
  • Loading branch information
tedivm authored Oct 7, 2019
2 parents f1bbf84 + ab780a6 commit aed09ea
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions src/JShrink/Minifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ class Minifier
*/
protected $input;

/**
* Length of input javascript.
*
* @var int
*/
protected $len = 0;

/**
* The location of the character (in the input string) that is next to be
* processed.
Expand Down Expand Up @@ -86,15 +93,15 @@ class Minifier
*
* @var array
*/
protected static $defaultOptions = array('flaggedComments' => true);
protected static $defaultOptions = ['flaggedComments' => true];

/**
* Contains lock ids which are used to replace certain code patterns and
* prevent them from being minified
*
* @var array
*/
protected $locks = array();
protected $locks = [];

/**
* Takes a string containing javascript and removes unneeded characters in
Expand All @@ -105,7 +112,7 @@ class Minifier
* @throws \Exception
* @return bool|string
*/
public static function minify($js, $options = array())
public static function minify($js, $options = [])
{
try {
ob_start();
Expand Down Expand Up @@ -157,21 +164,34 @@ protected function minifyDirectToOutput($js, $options)
protected function initialize($js, $options)
{
$this->options = array_merge(static::$defaultOptions, $options);
$js = str_replace("\r\n", "\n", $js);
$js = str_replace('/**/', '', $js);
$this->input = str_replace("\r", "\n", $js);
$this->input = str_replace(["\r\n", '/**/', "\r"], ["\n", "", "\n"], $js);

// We add a newline to the end of the script to make it easier to deal
// with comments at the bottom of the script- this prevents the unclosed
// comment error that can otherwise occur.
$this->input .= PHP_EOL;

// save input length to skip calculation every time
$this->len = strlen($this->input);

// Populate "a" with a new line, "b" with the first character, before
// entering the loop
$this->a = "\n";
$this->b = $this->getReal();
}

/**
* Characters that can't stand alone preserve the newline.
*
* @var array
*/
protected $noNewLineCharacters = [
'(' => true,
'-' => true,
'+' => true,
'[' => true,
'@' => true];

/**
* The primary action occurs here. This function loops through the input string,
* outputting anything that's relevant and discarding anything that is not.
Expand All @@ -183,7 +203,7 @@ protected function loop()
// new lines
case "\n":
// if the next line is something that can't stand alone preserve the newline
if ($this->b !== false && strpos('(-+[@', $this->b) !== false) {
if ($this->b !== false && isset($this->noNewLineCharacters[$this->b])) {
echo $this->a;
$this->saveString();
break;
Expand Down Expand Up @@ -226,7 +246,7 @@ protected function loop()
break;
}

// no break
// no break
default:
// check for some regex that breaks stuff
if ($this->a === '/' && ($this->b === '\'' || $this->b === '"')) {
Expand Down Expand Up @@ -257,6 +277,7 @@ protected function loop()
protected function clean()
{
unset($this->input);
$this->len = 0;
$this->index = 0;
$this->a = $this->b = '';
unset($this->c);
Expand All @@ -276,7 +297,7 @@ protected function getChar()
unset($this->c);
} else {
// Otherwise we start pulling from the input.
$char = substr($this->input, $this->index, 1);
$char = $this->index < $this->len ? $this->input[$this->index] : false;

// If the next character doesn't exist return false.
if (isset($char) && $char === false) {
Expand All @@ -289,7 +310,7 @@ protected function getChar()

// Normalize all whitespace except for the newline character into a
// standard space.
if ($char !== "\n" && ord($char) < 32) {
if ($char !== "\n" && $char < "\x20") {
return ' ';
}

Expand Down Expand Up @@ -340,7 +361,7 @@ protected function getReal()
*/
protected function processOneLineComments($startIndex)
{
$thirdCommentString = substr($this->input, $this->index, 1);
$thirdCommentString = $this->index < $this->len ? $this->input[$this->index] : false;

// kill rest of line
$this->getNext("\n");
Expand Down Expand Up @@ -429,7 +450,7 @@ protected function getNext($string)
$this->index = $pos;

// Return the first character of that string.
return substr($this->input, $this->index, 1);
return $this->index < $this->len ? $this->input[$this->index] : false;
}

/**
Expand Down Expand Up @@ -557,7 +578,7 @@ protected function lock($js)
/* lock things like <code>"asd" + ++x;</code> */
$lock = '"LOCK---' . crc32(time()) . '"';

$matches = array();
$matches = [];
preg_match('/([+-])(\s+)([+-])/S', $js, $matches);
if (empty($matches)) {
return $js;
Expand Down

0 comments on commit aed09ea

Please sign in to comment.