Skip to content
Closed
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
26 changes: 15 additions & 11 deletions packages/cli-kit/src/public/node/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -626,20 +626,24 @@ export type EOL = '\r\n' | '\n'
* @returns The detected end-of-line marker
*/
export function detectEOL(content: string): EOL {
const match = content.match(/\r\n|\n/g)

if (!match) {
return defaultEOL()
// Optimization: Use a single loop to count occurrences instead of .match().
// This avoids creating a potentially large intermediate array of matches.
let crlf = 0
let lf = 0

for (let i = 0; i < content.length; i++) {
if (content[i] === '\n') {
if (content[i - 1] === '\r') {
crlf++
} else {
lf++
}
}
}

// Optimization: Use a single loop to count occurrences instead of multiple .filter() calls.
// This reduces iterations from 2 to 1 and avoids creating intermediate arrays.
// For large files, this can be ~35-40% faster.
let crlf = 0
for (const eol of match) {
if (eol === '\r\n') crlf++
if (crlf === 0 && lf === 0) {
return defaultEOL()
}
const lf = match.length - crlf

return crlf > lf ? '\r\n' : '\n'
}
Expand Down
Loading