Skip to content

Conversation

@sapphi-red
Copy link
Member

@sapphi-red sapphi-red commented Sep 14, 2025

Inline single use variables in for statements:

function wrapper1() { var x = foo; for (var i = x; i < 10; i++) console.log(i) }
function wrapper2() { var i, x = foo; for (i = x; i < 10; i++) console.log(i) }
// to
function wrapper1() { for (var i = foo; i < 10; i++) console.log(i) }
function wrapper2() { var i; for (i = foo; i < 10; i++) console.log(i) }
function wrapper() { var x = {}; for (var a in x) console.log(a) }
// to
function wrapper() { for (var a in {}) console.log(a) }
function wrapper() { var x = []; for (var a of x) console.log(a) }
// to
function wrapper() { for (var a of []) console.log(a) }

refs #13277

@github-actions github-actions bot added A-minifier Area - Minifier C-enhancement Category - New feature or request labels Sep 14, 2025
Copy link
Member Author


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@sapphi-red sapphi-red requested a review from Boshen September 14, 2025 10:46
@sapphi-red sapphi-red marked this pull request as ready for review September 14, 2025 10:46
Copilot AI review requested due to automatic review settings September 14, 2025 10:46
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR extends the single use variable inlining optimization to support for-in and for-of statements. The minifier can now inline variables that are used only once in the right-hand side expression of these loop constructs.

  • Adds single use variable substitution for for-in and for-of statement right-hand side expressions
  • Includes proper handling of variable declaration initializers in for-in statements (Annex B.3.5)
  • Updates test coverage with new test cases for both for-in and for-of scenarios

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
crates/oxc_minifier/src/peephole/minimize_statements.rs Implements single use variable substitution for for-in and for-of statements
crates/oxc_minifier/tests/peephole/inline_single_use_variable.rs Adds test cases for the new optimization
tasks/track_memory_allocations/allocs_minifier.snap Updates memory allocation benchmarks
tasks/minsize/minsize.snap Updates minification size benchmarks

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@sapphi-red sapphi-red marked this pull request as draft September 14, 2025 10:48
@codspeed-hq
Copy link

codspeed-hq bot commented Sep 14, 2025

CodSpeed Instrumentation Performance Report

Merging #13755 will not alter performance

Comparing 09-14-feat_minifier_support_for-in_and_for-of_statements_for_single_use_variable_inlining (c364ad1) with main (c868796)1

Summary

✅ 37 untouched

Footnotes

  1. No successful run was found on main (c364ad1) during the generation of this report, so c868796 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@sapphi-red sapphi-red changed the title feat(minifier): support for-in and for-of statements for single use variable inlining feat(minifier): support for statements for single use variable inlining Sep 14, 2025
@sapphi-red sapphi-red changed the title feat(minifier): support for statements for single use variable inlining feat(minifier): support ForStatements for single use variable inlining Sep 14, 2025
@sapphi-red sapphi-red force-pushed the 09-14-feat_minifier_support_for-in_and_for-of_statements_for_single_use_variable_inlining branch from 6a8c861 to a823891 Compare September 14, 2025 10:59
@sapphi-red sapphi-red marked this pull request as ready for review September 14, 2025 11:02
@Boshen Boshen added the 0-merge Merge with Graphite Merge Queue label Sep 14, 2025
Copy link
Member

Boshen commented Sep 14, 2025

Merge activity

#13755)

Inline single use variables in for statements:
```js
function wrapper1() { var x = foo; for (var i = x; i < 10; i++) console.log(i) }
function wrapper2() { var i, x = foo; for (i = x; i < 10; i++) console.log(i) }
// to
function wrapper1() { for (var i = foo; i < 10; i++) console.log(i) }
function wrapper2() { var i; for (i = foo; i < 10; i++) console.log(i) }
```
```js
function wrapper() { var x = {}; for (var a in x) console.log(a) }
// to
function wrapper() { for (var a in {}) console.log(a) }
```
```js
function wrapper() { var x = []; for (var a of x) console.log(a) }
// to
function wrapper() { for (var a of []) console.log(a) }
```

refs #13277
@graphite-app graphite-app bot force-pushed the 09-14-feat_minifier_support_for-in_and_for-of_statements_for_single_use_variable_inlining branch from a823891 to c364ad1 Compare September 14, 2025 12:55
@graphite-app graphite-app bot merged commit c364ad1 into main Sep 14, 2025
25 checks passed
@graphite-app graphite-app bot deleted the 09-14-feat_minifier_support_for-in_and_for-of_statements_for_single_use_variable_inlining branch September 14, 2025 13:01
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Sep 14, 2025
graphite-app bot pushed a commit that referenced this pull request Sep 15, 2025
…s to for statement initializers (#13769)

```js
function wrapper() { var x = a; for (let a of x) console.log(a) }
```
should not be compress to
```js
function wrapper() { for (let a of a) console.log(a) }
```
because `a` points to a different variable as the scope is different.
This PR fixes those kinds of cases for all for statements.

Found in rolldown/rolldown#6220

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

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants