You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Here the data (e.g. `new Buffer('$5\r\nHello\r\n'`)) is passed to the parser and the result is passed to either function depending on the provided data.
74
-
parser.execute(buffer);
49
+
constParser=require("redis-parser");
50
+
51
+
classLibrary {
52
+
returnReply(reply) { /* ... */ }
53
+
returnError(err) { /* ... */ }
54
+
returnFatalError(err) { /* ... */ }
55
+
56
+
streamHandler() {
57
+
this.stream.on('data', (buffer) => {
58
+
// Here the data (e.g. `Buffer.from('$5\r\nHello\r\n'`))
59
+
// is passed to the parser and the result is passed to
60
+
// either function depending on the provided data.
61
+
parser.execute(buffer);
75
62
});
76
-
};
63
+
}
64
+
}
65
+
66
+
constlib=newLibrary();
67
+
68
+
constparser=newParser({
69
+
returnReply(reply) {
70
+
lib.returnReply(reply);
71
+
},
72
+
returnError(err) {
73
+
lib.returnError(err);
74
+
},
75
+
returnFatalError(err) {
76
+
lib.returnFatalError(err);
77
+
}
78
+
});
77
79
```
80
+
78
81
You do not have to use the returnFatalError function. Fatal errors will be returned in the normal error function in that case.
79
82
80
83
And if you want to return buffers instead of strings, you can do this by adding the `returnBuffers` option.
@@ -84,15 +87,15 @@ If you handle with big numbers that are to large for JS (Number.MAX_SAFE_INTEGER
84
87
```js
85
88
// Same functions as in the first example
86
89
87
-
var parser =newParser({
88
-
returnReply:function(reply) {
89
-
lib.returnReply(reply);
90
-
},
91
-
returnError:function(err) {
92
-
lib.returnError(err);
93
-
},
94
-
returnBuffers:true, // All strings are returned as Buffer e.g. <Buffer 48 65 6c 6c 6f>
95
-
stringNumbers:true// All numbers are returned as String
90
+
constparser=newParser({
91
+
returnReply(reply) {
92
+
lib.returnReply(reply);
93
+
},
94
+
returnError(err) {
95
+
lib.returnError(err);
96
+
},
97
+
returnBuffers:true, // All strings are returned as Buffer e.g. <Buffer 48 65 6c 6c 6f>
98
+
stringNumbers:true// All numbers are returned as String
96
99
});
97
100
98
101
// The streamHandler as above
@@ -112,50 +115,50 @@ The parser is highly optimized but there may still be further optimizations poss
112
115
113
116
Currently the benchmark compares the performance against the hiredis parser:
114
117
115
-
HIREDIS: $ multiple chunks in a bulk string x 859,880 ops/sec ±1.22% (82 runs sampled)
116
-
HIREDIS BUF: $ multiple chunks in a bulk string x 608,869 ops/sec ±1.72% (85 runs sampled)
117
-
JS PARSER: $ multiple chunks in a bulk string x 910,590 ops/sec ±0.87% (89 runs sampled)
118
-
JS PARSER BUF: $ multiple chunks in a bulk string x 1,299,507 ops/sec ±2.18% (84 runs sampled)
118
+
HIREDIS: $ multiple chunks in a bulk string x 994,387 ops/sec ±0.22% (554 runs sampled)
119
+
JS PARSER: $ multiple chunks in a bulk string x 1,010,728 ops/sec ±0.28% (559 runs sampled)
120
+
HIREDIS BUF: $ multiple chunks in a bulk string x 648,742 ops/sec ±0.80% (526 runs sampled)
121
+
JS PARSER BUF: $ multiple chunks in a bulk string x 1,728,849 ops/sec ±0.41% (555 runs sampled)
119
122
120
-
HIREDIS: + multiple chunks in a string x 1,787,203 ops/sec ±0.58% (96 runs sampled)
121
-
HIREDIS BUF: + multiple chunks in a string x 943,584 ops/sec ±1.62% (87 runs sampled)
122
-
JS PARSER: + multiple chunks in a string x 2,008,264 ops/sec ±1.01% (91 runs sampled)
123
-
JS PARSER BUF: + multiple chunks in a string x 2,045,546 ops/sec ±0.78% (91 runs sampled)
123
+
HIREDIS: + multiple chunks in a string x 1,861,132 ops/sec ±0.18% (564 runs sampled)
124
+
JS PARSER: + multiple chunks in a string x 2,131,892 ops/sec ±0.31% (558 runs sampled)
125
+
HIREDIS BUF: + multiple chunks in a string x 965,132 ops/sec ±0.58% (521 runs sampled)
126
+
JS PARSER BUF: + multiple chunks in a string x 2,304,482 ops/sec ±0.31% (559 runs sampled)
0 commit comments