Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion crates/oxc_formatter/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,14 @@ pub fn format<'ast>(
context: FormatContext<'ast>,
arguments: Arguments<'_, 'ast>,
) -> FormatResult<Formatted<'ast>> {
// Pre-allocate buffer at 40% of source length (source_len * 2 / 5).
// Analysis of 4,891 VSCode files shows FormatElement buffer length is typically 19% of source (median),
// with 95th percentile at 30-38% across all file sizes. This 0.4x multiplier avoids
// reallocation for 95%+ of files.
let capacity = (context.source_text().len() * 2) / 5;

let mut state = FormatState::new(context);
let mut buffer = VecBuffer::with_capacity(arguments.items().len(), &mut state);
let mut buffer = VecBuffer::with_capacity(capacity, &mut state);

buffer.write_fmt(arguments)?;

Expand Down
Loading