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

Encoding

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.

Decoding

For each placeholder char + replacement string combo in the replacement map, replace all matching placeholder chars in the source with the replacement string.

Credits

While this code isn't based on @aivopaas' Javascript crusher, which I didn't even know existed until after the fact, it was inspired by the "crushed" source of his JS1k demo, which motivated me to try to roll my own version.