Skip to content

Conversation

@armano2
Copy link

@armano2 armano2 commented Dec 4, 2025

Improve folding of iife

fixes #13651

details

  • check all CallExpression if they can be collapsed
    eg. passed as arguments or in assignment
  • functions without arguments and body can be collapsed to to void 0
    (() => {})() or (function () { })() to void 0
  • with one statement and without return will be collapsed to (c, void 0)
    (() => { fn() })() to (fn(), void 0)
  • FunctionExpression is not supported
    (function () { ... })()

notes

  • function with arguments could be handled by first in-lining them
  • empty function with arguments could be also if all arguments do not have defaults
  • we should consider optimizing function definitions by removing not used arguments
  • initially i added support for FunctionExpression but there is no way to track this usage, and thus we are unable to minify this

change example

code

{
  let AOT = true;
  const getAOT = /* @__NO_SIDE_EFFECTS__ */ () => AOT;
  const fn = getAOT() ? () => console.log("Hi") : () => {};
  fn();
}

currently is collapsed to

(/* @__PURE__ */ /* @__NO_SIDE_EFFECTS__ */ (() => !0)() ? () => console.log("Hi") : () => {})();

and with this change

console.log('Hi');

from what i can see @sapphi-red worked on this file

@github-actions github-actions bot added A-minifier Area - Minifier C-bug Category - Bug labels Dec 4, 2025
@armano2 armano2 marked this pull request as ready for review December 4, 2025 02:54
@codspeed-hq
Copy link

codspeed-hq bot commented Dec 4, 2025

CodSpeed Performance Report

Merging #16477 will not alter performance

Comparing armano2:fix/remove-unused-iife-expressions (7084a71) with main (514c724)

Summary

✅ 38 untouched
⏩ 7 skipped1

Footnotes

  1. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@armano2 armano2 marked this pull request as draft December 4, 2025 22:27
@armano2 armano2 marked this pull request as ready for review December 4, 2025 22:27
test(
"function wrapper() { let foo = {}; const bar = (() => { console.log() })(); foo.bar = bar; return foo }",
"function wrapper() { let foo = {}; return foo.bar = (() => { console.log() })(), foo }",
"function wrapper() { let foo = {}, bar = (console.log(), void 0); return foo.bar = void 0, foo; }",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this result is surprisingly longer, but i think this is correct, bar should be removed here

@sapphi-red sapphi-red changed the title fix(minifier): fold iife arrow functions in call expressions feat(minifier): fold iife arrow functions in call expressions Dec 5, 2025
@github-actions github-actions bot added the C-enhancement Category - New feature or request label Dec 5, 2025
@armano2
Copy link
Author

armano2 commented Dec 5, 2025

i noticed a bug in this implementation
this is not handled inside FunctionExpression, i was hoping to used references, but it looks like they this is not defined as such

@armano2 armano2 marked this pull request as draft December 5, 2025 21:14
@armano2 armano2 force-pushed the fix/remove-unused-iife-expressions branch from afa595c to 6d39973 Compare December 5, 2025 21:31
@armano2 armano2 marked this pull request as ready for review December 5, 2025 21:31
@armano2
Copy link
Author

armano2 commented Dec 5, 2025

@sapphi-red thank you for running tests issues reported by it should be fixed

is there any guide what tests we have to run localy?

@sapphi-red
Copy link
Member

sapphi-red commented Dec 6, 2025

Running the tests ran by the CI should be fine.

@armano2
Copy link
Author

armano2 commented Dec 6, 2025

Conformance tests should not be ok, its interesting that

  • Object.getPrototypeOf(async function () {}()) = Promise
  • Object.getPrototypeOf(async function* () {}()) = AsyncGenerator
  • Object.getPrototypeOf(async function* () {}()) = Generator

i'm not sure tho if this is this is the best way to check if we can collapse pife/iife by evaluating ancesotrs and checking if they are blocks

i did some cleanup and sorry for keeping it open while it was still WIP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-minifier Area - Minifier C-bug Category - Bug C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

minifier: inline functions that return a const value

2 participants