Skip to content

Commit 212eb8a

Browse files
committed
child_process: fix O(n*m) scan of cmd string
Don't scan the whole string for a "NODE_" substring, just check that the string starts with the expected prefix. This is a reprise of dbbfbe7 but this time for the child_process module.
1 parent dbbfbe7 commit 212eb8a

1 file changed

Lines changed: 6 additions & 8 deletions

File tree

lib/child_process.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -305,19 +305,17 @@ function getSocketList(type, slave, key) {
305305
return socketList;
306306
}
307307

308+
var INTERNAL_PREFIX = 'NODE_';
308309
function handleMessage(target, message, handle) {
309-
//Filter out internal messages
310-
//if cmd property begin with "_NODE"
310+
var eventName = 'message';
311311
if (message !== null &&
312312
typeof message === 'object' &&
313313
typeof message.cmd === 'string' &&
314-
message.cmd.indexOf('NODE_') === 0) {
315-
target.emit('internalMessage', message, handle);
316-
}
317-
//Non-internal message
318-
else {
319-
target.emit('message', message, handle);
314+
message.cmd.length > INTERNAL_PREFIX.length &&
315+
message.cmd.slice(0, INTERNAL_PREFIX.length) === INTERNAL_PREFIX) {
316+
eventName = 'internalMessage';
320317
}
318+
target.emit(eventName, message, handle);
321319
}
322320

323321
function setupChannel(target, channel) {

0 commit comments

Comments
 (0)