Just a little something I'm wasting my time with.
- this is a work in progress, so YMMV
- probably only useful for not-too-small, but not-too-big bits of code
- run it as
packify('code')in the console
- code should be pre-minified or closure compiled
- unnecessary whitespace, semicolons, comments, etc should be removed beforehand
- code should use
"wherever possible (avoid
', as it needs to be escaped)
- code should have lots of repeated strings, so reuse vars wherever possible
- project page, "packifier" web app, lecture tour coming soon
How it works
- The input source is scanned for repeated strings between 2-20 chars in length, and an array is built. Each array item consists of the string and the number of occurrences. Items that won't save characters are discarded.
- The array is sorted based on items that will yield the highest character savings (given the current state of the source).
- The first array item, with the highest potential character savings, is shifted off the array.
- Replace that item's string in the source with the next unused placeholder char (ASCII 1-31 and 127, excluding 10 and 13).
- That ASCII character + the replacement string is appended to the replacement map.
- Repeat steps 2-5 until there are no more placeholder ASCII chars or possible replacement strings.
- For each placeholder char + replacement string combo in the replacement map, replace all matching placeholder chars in the source with the replacement string.
Special thanks to Kyle Simpson for being the catalyst.
Copyright (c) 2010 "Cowboy" Ben Alman
Dual licensed under the MIT and GPL licenses.