Skip to content

Commit f6da450

Browse files
committed
small rewrite of hiredis parser - around 3% perf boost, not much really...
1 parent e855650 commit f6da450

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

lib/hiredis.js

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,45 @@
22

33
var hiredis = require('hiredis');
44

5-
function HiredisReplyParser(options) {
6-
this.name = 'hiredis';
7-
this.options = options;
8-
this.reader = new hiredis.Reader(options);
5+
/**
6+
* Parse data
7+
* @param parser
8+
* @returns {*}
9+
*/
10+
function parseData(parser) {
11+
try {
12+
return parser.reader.get();
13+
} catch (err) {
14+
// Protocol errors land here
15+
// Reset the parser. Otherwise new commands can't be processed properly
16+
parser.reader = new hiredis.Reader(parser.options);
17+
parser.returnFatalError(err);
18+
}
919
}
1020

11-
HiredisReplyParser.prototype.parseData = function () {
12-
try {
13-
return this.reader.get();
14-
} catch (err) {
15-
// Protocol errors land here
16-
// Reset the parser. Otherwise new commands can't be processed properly
17-
this.reader = new hiredis.Reader(this.options);
18-
this.returnFatalError(err);
19-
}
20-
};
21+
/**
22+
* Hiredis Parser
23+
* @param options
24+
* @constructor
25+
*/
26+
function HiredisReplyParser(options) {
27+
this.name = 'hiredis';
28+
this.options = options;
29+
this.reader = new hiredis.Reader(options);
30+
}
2131

2232
HiredisReplyParser.prototype.execute = function (data) {
23-
this.reader.feed(data);
24-
var reply = this.parseData();
33+
this.reader.feed(data);
34+
var reply = parseData(this);
2535

26-
while (reply !== undefined) {
27-
if (reply && reply.name === 'Error') {
28-
this.returnError(reply);
29-
} else {
30-
this.returnReply(reply);
31-
}
32-
reply = this.parseData();
33-
}
36+
while (reply !== undefined) {
37+
if (reply && reply.name === 'Error') {
38+
this.returnError(reply);
39+
} else {
40+
this.returnReply(reply);
41+
}
42+
reply = parseData(this);
43+
}
3444
};
3545

3646
module.exports = HiredisReplyParser;

0 commit comments

Comments
 (0)