Skip to content

Commit

Permalink
Some tiny optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd-Eden committed Oct 13, 2011
1 parent a75670c commit 1d66b6b
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,13 @@ exports.encodePacket = function (packet) {
}

// construct packet with required fragments
var encoded = [
type
, id + (ack == 'data' ? '+' : '')
, endpoint
];
var encoded = type + ':' + id + (ack == 'data' ? '+' : '') + ':' + endpoint;

// data fragment is optional
if (data !== null && data !== undefined)
encoded.push(data);
encoded += ':' + data;

return encoded.join(':');
return encoded;
};

/**
Expand Down Expand Up @@ -143,9 +139,20 @@ exports.encodePayload = function (packets) {

var regexp = /([^:]+):([0-9]+)?(\+)?:([^:]+)?:?([\s\S]*)?/;

/**
* Wrap the JSON.parse in a seperate function the crankshaft optimizer will
* only punish this function for the usage for try catch
*
* @api private
*/

function parse (data) {
try { return JSON.parse(data) }
catch (e) { return false }
}

exports.decodePacket = function (data) {
var pieces = data.match(regexp)
, parse = JSON.parse;
var pieces = data.match(regexp);

if (!pieces) return {};

Expand All @@ -172,34 +179,31 @@ exports.decodePacket = function (data) {
break;

case 'event':
try {
pieces = parse(data);
pieces = parse(data);
if (pieces) {
packet.name = pieces.name;
packet.args = pieces.args;
} catch (e) { }
}

packet.args = packet.args || [];
break;

case 'json':
try {
packet.data = parse(data);
} catch (e) { }
packet.data = parse(data);
break;

case 'connect':
packet.qs = data || '';
break;

case 'ack':
pieces = data.match(/^([0-9]+)(\+)?(.*)/);
if (pieces) {
packet.ackId = pieces[1];
packet.args = [];

if (pieces[3]) {
try {
packet.args = pieces[3] ? parse(pieces[3]) : [];
} catch (e) { }
packet.args = parse(pieces[3]) || [];
}
}
break;
Expand Down

0 comments on commit 1d66b6b

Please sign in to comment.