tedious / JShrink

Javascript Minifier built in PHP
http://www.tedivm.com
BSD 3-Clause "New" or "Revised" License
749 stars 152 forks source link

Replace "lock" preg_replace with state machine enhancements #114

Open tedivm opened 1 year ago

tedivm commented 1 year ago

The "lock" functionality explicitly locks certain patterns in place, such as "asd" + ++x;. If the plus signs in that example were to merge into one string ("asd"+++x;) it would cause an error with the javascript.

When this bug was discovered we took the easy way out and replaced the pattern with a special string. This was done with a preg_replace across the whole string. The performance on this is pretty garbage.

To resolve this properly the "+ ++" pattern would be detected inside of the state machine that generates the minified string. Placing it in there would prevent the additional preg_replace scan, and would allow us to skip the "add back" portion of the code. This would provide a significant performance boost.