Skip to content
Draft
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2026-05-20 - Optimize linesToColumns
**Learning:** Utilities that handle CLI output formatting, like `linesToColumns`, often call expensive regex-based helpers (e.g., `unstyled` for ANSI strip) multiple times for the same input. Caching these lengths in a single pass over the data significantly reduces overhead.
**Action:** Always check if string measurement utilities are called repeatedly in loops or nested maps, and use pre-calculation/caching to avoid redundant work.
25 changes: 19 additions & 6 deletions packages/cli-kit/src/public/common/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,29 @@ export function tryParseInt(maybeInt: string | undefined): number | undefined {
* @returns A string with the columns aligned.
*/
export function linesToColumns(lines: string[][]): string {
if (lines.length === 0 || !lines[0]) return ''

const widths: number[] = []
for (let i = 0; lines[0] && i < lines[0].length; i++) {
const columnRows = lines.map((line) => line[i]!)
widths.push(Math.max(...columnRows.map((row) => unstyled(row).length)))
const unstyledLengths: number[][] = []

// Pre-calculate unstyled lengths and column widths in a single pass
for (let i = 0; i < lines.length; i++) {
const line = lines[i]!
unstyledLengths[i] = []
for (let j = 0; j < line.length; j++) {
const cell = line[j]!
const length = unstyled(cell).length
unstyledLengths[i]![j] = length
widths[j] = Math.max(widths[j] ?? 0, length)
}
}

const paddedLines = lines
.map((line) => {
.map((line, rowIndex) => {
return line
.map((col, index) => {
return `${col}${' '.repeat(widths[index]! - unstyled(col).length)}`
.map((cell, colIndex) => {
const padding = ' '.repeat(widths[colIndex]! - unstyledLengths[rowIndex]![colIndex]!)
return `${cell}${padding}`
})
.join(' ')
.trimEnd()
Expand Down
Loading