From 212eb8a52e835e3a8e71742fc557b0f4a43bf21c Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 11 Apr 2013 13:50:45 +0200 Subject: [PATCH] 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. --- lib/child_process.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/child_process.js b/lib/child_process.js index e4fe26ecda..1b6b8b21d6 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -305,19 +305,17 @@ function getSocketList(type, slave, key) { return socketList; } +var INTERNAL_PREFIX = 'NODE_'; function handleMessage(target, message, handle) { - //Filter out internal messages - //if cmd property begin with "_NODE" + var eventName = 'message'; if (message !== null && typeof message === 'object' && typeof message.cmd === 'string' && - message.cmd.indexOf('NODE_') === 0) { - target.emit('internalMessage', message, handle); - } - //Non-internal message - else { - target.emit('message', message, handle); + message.cmd.length > INTERNAL_PREFIX.length && + message.cmd.slice(0, INTERNAL_PREFIX.length) === INTERNAL_PREFIX) { + eventName = 'internalMessage'; } + target.emit(eventName, message, handle); } function setupChannel(target, channel) {