Skip to content

Closure minification adds \uxxxx escapes into output file, increasing code size #4158

Closed

Description

@juj

In emscripten-core/emscripten#21426 we are discussing ways to improve on Base64 encoding of binary WebAssembly Modules embedded inside .js code. It is observed that both gzip and brotli compress Base64 pessimistically.

One observation here is that the UTF-8 standard is well-specified, so we can attempt to embed bytes directly as UTF-8 code points.

Attempting to do so runs into a Closure minification problem however.

Input: ab.zip

function binaryDecode(r) {
  for(var t=0, B=r.length, e=new Uint8Array(B); t<B; ++t) e[t]=r.charCodeAt(t)-1;
  return e;
}

// String with bytes 0x00 - 0xFF embedded in it.
var js = '��������	\n�\r������������������ !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~��������������������������������� ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿĀ';

var a = binaryDecode(js);
console.log(a.slice(0, 64));
console.log(a.slice(64, 128));
console.log(a.slice(128, 192));
console.log(a.slice(192, 256));

This code nicely prints out all bytes from 0x00 up to 0xFF.

Input file is 689 bytes in size. However, running this file through Closure compiler Advanced Optimizations produces a file that is 1225 bytes in size: ab_closured.zip

Online Closure link

Activity

  1. changed the title [-]Closure minification adds \uxxxx espaces into output file, increasing code size[/-] [+]Closure minification adds \uxxxx escapes into output file, increasing code size[/+] on Mar 5, 2024
  2. lauraharker commented on Mar 5, 2024

    @lauraharker
    Contributor

    The compiler defaults to outputting ASCII, but you can specify a different output charset via the --charset flag. (https://github.com/google/closure-compiler/wiki/Flags-and-Options#miscellaneous).

    Does --charset=UTF-8 work for you?

  3. juj commented on Mar 6, 2024

    @juj
    Author

    Thanks, yeah, that works!

  4. added a commit that references this issue on Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions