From 339d9e05e25674432fa4beb6b67ffa2b00df833f Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Thu, 8 Oct 2026 13:15:27 -0700 Subject: [PATCH] Replace `stripDefaultUndefined` with Terser `--compress` Instead of a dedicated `stripDefaultUndefined` Acorn AST pass (added in compression (`{defaults: false, evaluate: true, keep_fargs: false}`) when minifying JS without Closure Compiler. In addition to stripping `= undefined` default parameter values, this performs lightweight Terser cleanups (`undefined` -> `void 0`, single-statement block brace removal, and constant folding). In general, this prevents us from needing to replicate compression passes that already exist in upstream Terser (see #27871). Timing highlights (`test/hello_world.c` with `-sINCLUDE_FULL_LIBRARY`, ~252 KB minified JS): - `minify_sync` with `compress: false`: 105.5 ms - `minify_sync` with `compress: {defaults: false, evaluate: true, keep_fargs: false}`: 188.3 ms (+82.8 ms, or ~+63 ms net after removing the `stripDefaultUndefined` AST walk), saving ~1.4 KB. - On a standard `-O2` `hello_world.c` build (~8.5 KB JS), `minify_sync` goes from 3.2 ms to 5.5 ms (+2.3 ms). This change, in general, should prevent us from being tempted to replicate compression passes that already exist in upstream terser (See #27871, for example). See: #27839 --- .../test_codesize_file_preload.expected.js | 24 +++++++-------- .../test_codesize_hello_esm_integration.json | 8 ++--- ...test_codesize_minimal_esm_integration.json | 8 ++--- test/codesize/test_no_closure_code_size.json | 20 ++++++------- .../stripDefaultUndefined-output.js | 21 ------------- test/js_optimizer/stripDefaultUndefined.js | 20 ------------- test/test_codesize.py | 2 +- test/test_other.py | 1 - tools/acorn-optimizer.mjs | 30 ++++++------------- tools/building.py | 4 +-- 10 files changed, 41 insertions(+), 97 deletions(-) delete mode 100644 test/js_optimizer/stripDefaultUndefined-output.js delete mode 100644 test/js_optimizer/stripDefaultUndefined.js diff --git a/test/codesize/test_codesize_file_preload.expected.js b/test/codesize/test_codesize_file_preload.expected.js index 5e405b2184da7..e8d7a1cca2605 100644 --- a/test/codesize/test_codesize_file_preload.expected.js +++ b/test/codesize/test_codesize_file_preload.expected.js @@ -947,7 +947,7 @@ var MEMFS = { mount(mount) { return MEMFS.createNode(null, "/", 16895, 0); }, - createNode(parent, name, mode, dev) { + createNode(parent, name, mode, dev = undefined) { if (FS.isBlkdev(mode) || FS.isFIFO(mode)) { // not supported throw new FS.ErrnoError(63); @@ -1112,7 +1112,7 @@ var MEMFS = { } throw MEMFS.doesNotExistError; }, - mknod(parent, name, mode, dev) { + mknod(parent, name, mode, dev = undefined) { return MEMFS.createNode(parent, name, mode, dev); }, rename(old_node, new_dir, new_name) { @@ -1618,7 +1618,7 @@ var FS = { // if we failed to find it in the cache, call into the VFS return FS.lookup(parent, name); }, - createNode(parent, name, mode, rdev) { + createNode(parent, name, mode, rdev = undefined) { var node = new FS.FSNode(parent, name, mode, rdev); FS.hashAddNode(node); return node; @@ -2003,7 +2003,7 @@ var FS = { } } }, - mkdev(path, mode, dev) { + mkdev(path, mode, dev = undefined) { if (typeof dev === "undefined") { dev = mode; mode = 438; @@ -2457,7 +2457,7 @@ var FS = { stream.ungotten = []; return stream.position; }, - read(stream, buffer, offset, length, position) { + read(stream, buffer, offset, length, position = undefined) { if (length < 0 || position < 0) { throw new FS.ErrnoError(28); } @@ -2483,7 +2483,7 @@ var FS = { if (!seeking) stream.position += bytesRead; return bytesRead; }, - write(stream, buffer, offset, length, position, canOwn) { + write(stream, buffer, offset, length, position = undefined, canOwn = undefined) { if (length < 0 || position < 0) { throw new FS.ErrnoError(28); } @@ -2702,7 +2702,7 @@ var FS = { "MEMFS": MEMFS }; }, - init(input, output, error) { + init(input = undefined, output = undefined, error = undefined) { FS.initialized = true; // Allow Module.stdin etc. to provide defaults, if none explicitly passed to us here FS.createStandardStreams(input, output, error); @@ -2717,7 +2717,7 @@ var FS = { } } }, - createPath(parent, path, canRead, canWrite) { + createPath(parent, path, canRead = undefined, canWrite = undefined) { parent = typeof parent === "string" ? parent : FS.getPath(parent); var parts = path.split("/").reverse(); while (parts.length) { @@ -2733,12 +2733,12 @@ var FS = { } return current; }, - createFile(parent, name, properties, canRead, canWrite) { + createFile(parent, name, properties, canRead = undefined, canWrite = undefined) { var path = PATH.join2(typeof parent === "string" ? parent : FS.getPath(parent), name); var mode = FS_getMode(canRead, canWrite); return FS.create(path, mode); }, - createDataFile(parent, name, data, canRead, canWrite, canOwn) { + createDataFile(parent, name, data = undefined, canRead = undefined, canWrite = undefined, canOwn = undefined) { var path = name; if (parent) { parent = typeof parent === "string" ? parent : FS.getPath(parent); @@ -2756,7 +2756,7 @@ var FS = { FS.chmod(node, mode); } }, - createDevice(parent, name, input, output) { + createDevice(parent, name, input = undefined, output = undefined) { var path = PATH.join2(typeof parent === "string" ? parent : FS.getPath(parent), name); var mode = FS_getMode(!!input, !!output); FS.createDevice.major ??= 64; @@ -2823,7 +2823,7 @@ var FS = { } } }, - createLazyFile(parent, name, url, canRead, canWrite) { + createLazyFile(parent, name, url, canRead = undefined, canWrite = undefined) { // Lazy chunked Uint8Array (implements get and length from Uint8Array). // Actual getting is abstracted away for eventual reuse. class LazyUint8Array { diff --git a/test/codesize/test_codesize_hello_esm_integration.json b/test/codesize/test_codesize_hello_esm_integration.json index 5eca88194e87a..5302787627145 100644 --- a/test/codesize/test_codesize_hello_esm_integration.json +++ b/test/codesize/test_codesize_hello_esm_integration.json @@ -1,12 +1,12 @@ { "a.out.mjs": 671, "a.out.mjs.gz": 401, - "a.out.support.mjs": 6094, - "a.out.support.mjs.gz": 2390, + "a.out.support.mjs": 6068, + "a.out.support.mjs.gz": 2379, "a.out.nodebug.wasm": 1220, "a.out.nodebug.wasm.gz": 757, - "total": 7985, - "total_gz": 3548, + "total": 7959, + "total_gz": 3537, "sent": [ "a (fd_write)" ], diff --git a/test/codesize/test_codesize_minimal_esm_integration.json b/test/codesize/test_codesize_minimal_esm_integration.json index 42220372c71bb..9fd41cce932c0 100644 --- a/test/codesize/test_codesize_minimal_esm_integration.json +++ b/test/codesize/test_codesize_minimal_esm_integration.json @@ -1,12 +1,12 @@ { "a.out.mjs": 683, "a.out.mjs.gz": 410, - "a.out.support.mjs": 2965, - "a.out.support.mjs.gz": 1234, + "a.out.support.mjs": 2949, + "a.out.support.mjs.gz": 1227, "a.out.nodebug.wasm": 75, "a.out.nodebug.wasm.gz": 87, - "total": 3723, - "total_gz": 1731, + "total": 3707, + "total_gz": 1724, "sent": [], "imports": [], "exports": [ diff --git a/test/codesize/test_no_closure_code_size.json b/test/codesize/test_no_closure_code_size.json index 258736134cb6a..01f4ce40bc96c 100644 --- a/test/codesize/test_no_closure_code_size.json +++ b/test/codesize/test_no_closure_code_size.json @@ -1,20 +1,20 @@ { - "hello_world.js": 8058, - "hello_world.js.gz": 2932, + "hello_world.js": 8021, + "hello_world.js.gz": 2926, "hello_world.wasm": 1188, "hello_world.wasm.gz": 733, - "web.js": 6153, - "web.js.gz": 2277, + "web.js": 6128, + "web.js.gz": 2269, "web.wasm": 1188, "web.wasm.gz": 733, - "strict.js": 6181, - "strict.js.gz": 2341, + "strict.js": 6152, + "strict.js.gz": 2335, "strict.wasm": 1188, "strict.wasm.gz": 733, - "files.js": 54672, - "files.js.gz": 15388, + "files.js": 54296, + "files.js.gz": 15296, "files.wasm": 371, "files.wasm.gz": 264, - "total": 78999, - "total_gz": 25401 + "total": 78532, + "total_gz": 25289 } diff --git a/test/js_optimizer/stripDefaultUndefined-output.js b/test/js_optimizer/stripDefaultUndefined-output.js deleted file mode 100644 index 8dc0c2088618d..0000000000000 --- a/test/js_optimizer/stripDefaultUndefined-output.js +++ /dev/null @@ -1,21 +0,0 @@ -function foo(a, b, c = false, d) { - return a + b + c + d; -} - -var bar = (a, b, c = 123) => a + b + c; - -var single = a => a; - -var obj = { - method(a, b, c) { - return a + b + c; - } -}; - -function destruct({a, b = 1}, [c, d = 2]) { - return a + b + c + d; -} - -var x = undefined; - -x = undefined; diff --git a/test/js_optimizer/stripDefaultUndefined.js b/test/js_optimizer/stripDefaultUndefined.js deleted file mode 100644 index 1d715b992e673..0000000000000 --- a/test/js_optimizer/stripDefaultUndefined.js +++ /dev/null @@ -1,20 +0,0 @@ -function foo(a, b = undefined, c = false, d = undefined) { - return a + b + c + d; -} - -var bar = (a, b = undefined, c = 123) => a + b + c; - -var single = (a = undefined) => a; - -var obj = { - method(a, b = undefined, c = undefined) { - return a + b + c; - }, -}; - -function destruct({a = undefined, b = 1} = undefined, [c = undefined, d = 2] = undefined) { - return a + b + c + d; -} - -var x = undefined; -x = undefined; diff --git a/test/test_codesize.py b/test/test_codesize.py index e4e50d3549bab..4cae162a1a383 100644 --- a/test/test_codesize.py +++ b/test/test_codesize.py @@ -243,7 +243,7 @@ def run_codesize_test(self, filename, cflags, check_funcs=True, check_full_js=Fa outfile = 'a.out.mjs' if esm else 'a.out.js' js_file = 'a.out.support.mjs' if esm else outfile - # Run once without closure and parse output to find the JS->wasm imports. + # Run once without closure or terser minification and parse output to find the JS->wasm imports. build_cmd = [compiler_for(filename), filename, '-o', outfile, '--output-eol=linux', '--emit-minification-map=minify.map'] + cflags + self.get_cflags() self.run_process(build_cmd + ['-g2']) # find the imports we send from JS diff --git a/test/test_other.py b/test/test_other.py index 2db5c2be98429..c937834a55d04 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -3127,7 +3127,6 @@ def test_extern_prepost(self): 'JSDCE-fors': (['JSDCE'],), 'JSDCE-objectPattern': (['JSDCE'],), 'AJSDCE': (['AJSDCE'],), - 'stripDefaultUndefined': (['stripDefaultUndefined'],), 'emitDCEGraph': (['emitDCEGraph', '--no-print'],), 'emitDCEGraph-closure': (['emitDCEGraph', '--no-print', '--closure-friendly'], 'emitDCEGraph.js'), 'emitDCEGraph-dynCall': (['emitDCEGraph', '--no-print'],), diff --git a/tools/acorn-optimizer.mjs b/tools/acorn-optimizer.mjs index 7dba3aa51d73b..474b41c3aa684 100755 --- a/tools/acorn-optimizer.mjs +++ b/tools/acorn-optimizer.mjs @@ -410,25 +410,6 @@ function AJSDCE(ast) { JSDCE(ast, /* aggressive= */ true); } -// Strip `= undefined` default values from function parameters and destructuring -// patterns. These default values have no runtime semantic effect (since -// `undefined` is already the default), and are only needed by Closure Compiler -// for arity/type checking. -function stripDefaultUndefined(ast) { - fullWalk(ast, (node) => { - if ( - node.type === 'AssignmentPattern' && - node.right.type === 'Identifier' && - node.right.name === 'undefined' - ) { - const left = node.left; - delete node.left; - delete node.right; - Object.assign(node, left); - } - }); -} - function isWasmImportsAssign(node) { // var wasmImports = .. // or @@ -1822,6 +1803,7 @@ const { verbose, 'no-print': noPrint, 'minify-whitespace': minifyWhitespace, + compress, outfile, }, positionals: [infile, ...passes], @@ -1832,6 +1814,7 @@ const { verbose: {type: 'boolean'}, 'no-print': {type: 'boolean'}, 'minify-whitespace': {type: 'boolean'}, + compress: {type: 'boolean'}, outfile: {type: 'string', short: 'o'}, }, allowPositionals: true, @@ -1877,7 +1860,6 @@ if (closureFriendly) { const registry = { JSDCE, AJSDCE, - stripDefaultUndefined, applyImportAndExportNameChanges, emitDCEGraph, applyDCEGraphRemovals, @@ -1951,7 +1933,13 @@ if (!noPrint) { } else { output = minify_sync(ast, { parse: {spidermonkey: true}, - compress: false, + compress: compress + ? { + defaults: false, + evaluate: true, + keep_fargs: false, + } + : false, mangle: false, format, }).code; diff --git a/tools/building.py b/tools/building.py index 9b0baed46945f..666543c24de54 100644 --- a/tools/building.py +++ b/tools/building.py @@ -818,12 +818,10 @@ def minify_wasm_js(js_file, wasm_file, expensive_optimizations, debug_info): else: passes.append('JSDCE') - if not settings.USE_CLOSURE_COMPILER: - passes.append('stripDefaultUndefined') - # Don't minify if we are going to run closure compiler afterwards if settings.MINIFY_WHITESPACE and not settings.MAYBE_CLOSURE_COMPILER: passes.append('--minify-whitespace') + passes.append('--compress') if passes: logger.debug('running cleanup on shell code: ' + ' '.join(passes))