Skip to content

Commit e997177

Browse files
authored
perf: improve speed of minified object literals (#521)
1 parent 0b0d75b commit e997177

File tree

2 files changed

+1063
-2
lines changed

2 files changed

+1063
-2
lines changed

src/generation/generate.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2669,7 +2669,7 @@ fn gen_object_lit<'a>(node: &'a ObjectLit, context: &mut Context<'a>) -> PrintIt
26692669
prefer_hanging: context.config.object_expression_prefer_hanging,
26702670
prefer_single_line: context.config.object_expression_prefer_single_line,
26712671
force_single_line: false,
2672-
force_multi_line: false,
2672+
force_multi_line: is_node_definitely_above_line_width(node.range(), context),
26732673
surround_single_line_with_spaces: context.config.object_expression_space_surrounding_properties,
26742674
allow_blank_lines: true,
26752675
node_sorter: None,
@@ -4114,7 +4114,7 @@ fn gen_object_pat<'a>(node: &'a ObjectPat, context: &mut Context<'a>) -> PrintIt
41144114
prefer_hanging: context.config.object_pattern_prefer_hanging,
41154115
prefer_single_line: context.config.object_pattern_prefer_single_line,
41164116
force_single_line: false,
4117-
force_multi_line: false,
4117+
force_multi_line: is_node_definitely_above_line_width(node.range(), context),
41184118
surround_single_line_with_spaces: context.config.object_pattern_space_surrounding_properties,
41194119
allow_blank_lines: true,
41204120
node_sorter: None,
@@ -9369,6 +9369,24 @@ fn has_any_node_comment_on_different_line(nodes: &[impl SourceRanged], context:
93699369
}
93709370
}
93719371

9372+
fn is_node_definitely_above_line_width<'a>(range: SourceRange, context: &Context<'a>) -> bool {
9373+
let text = range.text_fast(context.program);
9374+
let max_width = context.config.line_width as usize * 2;
9375+
if text.len() < max_width {
9376+
return false;
9377+
}
9378+
let mut count = 0;
9379+
for c in text.chars() {
9380+
if !c.is_whitespace() {
9381+
count += 1;
9382+
if count > max_width {
9383+
return true;
9384+
}
9385+
}
9386+
}
9387+
false
9388+
}
9389+
93729390
/* config helpers */
93739391

93749392
fn get_generated_separator(separator: &Separator, is_trailing: bool, is_multi_line: &ConditionResolver) -> PrintItems {

0 commit comments

Comments
 (0)