Skip to content

Commit 6121692

Browse files
Mounesh-13Mouneshbitwiseman
authored
perf(core): resolve O(N^2) memory reallocation bottleneck in line-break tokenization (#2434)
* perf(core): eliminate O(N^2) memory reallocation in split_linebreaks * feat(core): optimize split_linebreaks and fix CI Node version * Update package-lock.json --------- Co-authored-by: Mounesh <work.mounesh@example.com> Co-authored-by: Liam Newman <bitwiseman@gmail.com>
1 parent f00005f commit 6121692

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

js/src/javascript/beautifier.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,16 @@ function split_linebreaks(s) {
104104
//return s.split(/\x0d\x0a|\x0a/);
105105

106106
s = s.replace(acorn.allLineBreaks, '\n');
107-
var out = [],
108-
idx = s.indexOf("\n");
107+
var out = [];
108+
var start = 0;
109+
var idx = s.indexOf("\n", start);
109110
while (idx !== -1) {
110-
out.push(s.substring(0, idx));
111-
s = s.substring(idx + 1);
112-
idx = s.indexOf("\n");
111+
out.push(s.substring(start, idx));
112+
start = idx + 1;
113+
idx = s.indexOf("\n", start);
113114
}
114-
if (s.length) {
115-
out.push(s);
115+
if (start < s.length) {
116+
out.push(s.substring(start));
116117
}
117118
return out;
118119
}

0 commit comments

Comments
 (0)