Skip to content

feat(minifier): simplify destructuring array assigment#16580

Open
armano2 wants to merge 56 commits intooxc-project:mainfrom
armano2:fix/inline-statements
Open

feat(minifier): simplify destructuring array assigment#16580
armano2 wants to merge 56 commits intooxc-project:mainfrom
armano2:fix/inline-statements

Conversation

@armano2
Copy link
Contributor

@armano2 armano2 commented Dec 8, 2025

@github-actions github-actions bot added A-minifier Area - Minifier C-enhancement Category - New feature or request labels Dec 8, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Dec 9, 2025

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing armano2:fix/inline-statements (21d1b2d) with main (645c3f0)1

Summary

✅ 38 untouched benchmarks
⏩ 7 skipped benchmarks2

Footnotes

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

  2. 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 changed the title feat(minifier): simplify assigment with array destructuring feat(minifier): simplify assigment with destructuring Dec 17, 2025
@armano2 armano2 marked this pull request as ready for review December 24, 2025 19:44
@armano2
Copy link
Contributor Author

armano2 commented Dec 25, 2025

ok, all changes applied, and it seem to be working fine

@armano2
Copy link
Contributor Author

armano2 commented Dec 25, 2025

q: what do you think about not processing vars if there is more than one non literal value?

// in this case only one variable will be created and we could create it only if its not first
decl.kind.is_var() &&
init_expr.elements.iter().filter(|elem| !elem.is_literal_value(false, ctx)).count() > 1

or even do not do that if there is any non literal

// in this case we never create variables
decl.kind.is_var() &&
init_expr.elements.iter().any(|elem| !elem.is_literal_value(false, ctx))

that would let us handle all cases where we extensively create additional variables for vars


this solves issues like:

var [a, b] = [1, 2], [a, b] = [b, a] // in
var a = 2, b = 1 // new
var a = 1, _ = 2, _2 = a, a = _, b = _2 // old
var [a, b] = [!d, !a] // in - out
var _ = !d, _2 = !a, a = _, b = _2 // old
var [a, b] = [b, a], [a, b] = [b, a] // in - out
var _3 = b, _4 = a, a = _3, b = _4, _ = b, _2 = a, a = _, b = _2 // old

@armano2 armano2 changed the title feat(minifier): simplify assigment with destructuring feat(minifier): simplify destructuring array assigment Dec 25, 2025
Comment on lines 491 to 513
if decl.kind.is_var() && init_len > 1 {
let binding_identifiers = decl.id.get_binding_identifiers();
if !binding_identifiers.is_empty() {
return !init_expr.elements.iter().any(|e| {
match e.as_expression() {
Some(Expression::NewExpression(_))
| Some(Expression::CallExpression(_)) => true,
Some(Expression::Identifier(ident)) => {
let Some(ref_symbol) =
&ctx.scoping().get_reference(ident.reference_id()).symbol_id()
else {
// global reference
return false;
};
// check whathever id is present in init [a] = [b]
binding_identifiers.iter().any(|id| id.symbol_id().eq(ref_symbol))
}
None => e.is_spread(), // do not allow spread
_ => !e.is_literal_value(false, ctx),
}
});
}
}
Copy link
Contributor Author

@armano2 armano2 Jan 2, 2026

Choose a reason for hiding this comment

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

maybe we should extract this to something more generic

ideally we would want to resolve it based on nodeId by comparing sub-tree, but that's currently not supported

this produces better output than creating intermediate variables that would otherwise could never be in-lined

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.

2 participants