Skip to content

Commit b7e014a

Browse files
authored
perf: reduce js_run_devserver cycle event parsing memory usage (#2274)
1 parent 8547937 commit b7e014a

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

js/private/js_run_devserver.mjs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -841,17 +841,22 @@ class AspectWatchProtocol {
841841

842842
async _receive(type = null) {
843843
return new Promise((resolve, reject) => {
844-
let line = ''
845-
const dataReceived = (data) => {
846-
line += data.toString()
847-
if (!line.endsWith('\n')) {
844+
const dataBufs = []
845+
const connection = this.connection
846+
847+
connection.on('data', function dataReceived(data) {
848+
dataBufs.push(data)
849+
850+
if (data.at(data.byteLength - 1) !== '\n'.charCodeAt(0)) {
848851
return
849852
}
850853

851-
this.connection.off('data', dataReceived)
854+
connection.off('data', dataReceived)
852855

853856
try {
854-
const msg = JSON.parse(line.trim())
857+
const msg = JSON.parse(
858+
Buffer.concat(dataBufs).toString().trim()
859+
)
855860
if (type && msg.kind !== type) {
856861
reject(
857862
new Error(
@@ -864,9 +869,7 @@ class AspectWatchProtocol {
864869
} catch (e) {
865870
reject(e)
866871
}
867-
}
868-
869-
this.connection.on('data', dataReceived)
872+
})
870873
})
871874
}
872875

0 commit comments

Comments
 (0)