Skip to content

Missed optimization with multiple optional chaining calls on undefined #1400

Open

Description

@rictic

This is a feature request

Version

5.18.0

Complete CLI command or minify() options used

{
  "module": true,
  "warnings": true,
  "ecma": 2021,
  "compress": {
    "passes": 4,
  },
  "mangle": {},
  "output": {},
  "parse": {},
  "rename": {},
}

terser input

const DEV_MODE = false;
const debugLog = DEV_MODE ? (v) => console.log(v) : undefined;

debugLog?.('one')
debugLog?.('two');
debugLog?.('three');

terser output

const o=void 0;o?.("one"),o?.("two"),o?.("three");

Expected result

This can be completely dead code eliminated. Interestingly, terser will dead code eliminate some similar programs.

If there's at most one call to debugLog, it will be compiled to zero.

If undefined is replaced with null, then terser will dead code up to two calls to debugLog.

If the syntax debugLog && debugLog('') is used instead, then any number of calls to debugLog will be compiled out.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions