From db2fcda655869ec999dc1e0dd1e6b0289410443f Mon Sep 17 00:00:00 2001 From: ansuz Date: Mon, 26 Oct 2015 22:11:20 -0400 Subject: [PATCH] ChainPadSrv.js : come back and clean up parseMessage just a little bit --- ChainPadSrv.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/ChainPadSrv.js b/ChainPadSrv.js index a21f43e3e..51e312540 100644 --- a/ChainPadSrv.js +++ b/ChainPadSrv.js @@ -24,16 +24,18 @@ var PING = 4; var PONG = 5; var parseMessage = function (msg) { - var orig=msg, - res ={}; + var res ={}; // two or more? use a for ['pass','user','channelId','content'].forEach(function(attr){ - var len=msg.substring(0,msg.indexOf(':')); - msg=msg.substring(len.length+1); - var prop=msg.substring(0,Number(len)); - msg = msg.substring(prop.length); - res[attr]=prop; + var len=msg.slice(0,msg.indexOf(':')), + // taking an offset lets us slice out the prop + // and saves us one string copy + o=len.length+1, + prop=res[attr]=msg.slice(o,Number(len)+o); + // slice off the property and its descriptor + msg = msg.slice(prop.length+o); }); + // content is the only attribute that's not a string res.content=JSON.parse(res.content); return res; };