Skip to content

Commit a68b68a

Browse files
author
xiaochen.gaoxc
committed
refactor: enhance decode performance
1 parent 1bea205 commit a68b68a

File tree

4 files changed

+201
-189
lines changed

4 files changed

+201
-189
lines changed

benchmark/decode.js

Lines changed: 154 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -70,163 +70,160 @@ var arrBuf2 = hessian.encode([1, 2, 3], '2.0');
7070
var arrObjectBuf1 = hessian.encode([1, "name", "xxx1231231231231xxx123"], '1.0');
7171
var arrObjectBuf2 = hessian.encode([1, "name", "xxx1231231231231xxx123"], '2.0');
7272

73-
suite
7473

75-
.add('hessian1 decode: number', function() {
76-
hessian.decode(number1Buf1, '1.0');
77-
})
78-
.add('hessian2 decode: number', function() {
79-
hessian.decode(number1Buf2, '2.0');
80-
})
81-
82-
.add('hessian1 decode: date', function() {
83-
hessian.decode(dateBuf1, '1.0');
84-
})
85-
.add('hessian2 decode: date', function() {
86-
hessian.decode(dateBuf2, '2.0');
87-
})
88-
89-
.add('hessian1 decode: long', function() {
90-
hessian.decode(longBuf1, '1.0');
91-
})
92-
.add('hessian2 decode: long', function() {
93-
hessian.decode(longBuf2, '2.0');
94-
})
95-
96-
.add('hessian1 decode: string', function() {
97-
hessian.decode(stringBuf1, '1.0');
98-
})
99-
.add('hessian2 decode: string', function() {
100-
hessian.decode(stringBuf2, '2.0');
101-
})
102-
103-
.add('hessian1 decode: [1, 2, 3]', function() {
104-
hessian.decode(arrBuf1, '1.0');
105-
})
106-
.add('hessian2 decode: [1, 2, 3]', function() {
107-
hessian.decode(arrBuf2, '2.0');
108-
})
109-
.add('hessian1 decode array', function() {
110-
hessian.decode(arrObjectBuf1, '1.0');
111-
})
112-
.add('hessian2 decode array', function() {
113-
hessian.decode(arrObjectBuf2, '2.0');
114-
})
115-
116-
.add('hessian1 decode: simple object', function() {
117-
hessian.decode(simpleObjectBuf1, '1.0');
118-
})
119-
.add('hessian2 decode: simple object', function() {
120-
hessian.decode(simpleObjectBuf2, '2.0');
121-
})
122-
123-
.add('hessian1 decode: complex object', function() {
124-
hessian.decode(complexObjectBuf1, '1.0');
125-
})
126-
.add('hessian2 decode: complex object', function() {
127-
hessian.decode(complexObjectBuf2, '2.0');
128-
})
129-
.add('hessian1 decode with type: number', function() {
130-
hessian.decode(number1Buf1, '1.0', true);
131-
})
132-
.add('hessian2 decode with type: number', function() {
133-
hessian.decode(number1Buf2, '2.0', true);
134-
})
135-
136-
.add('hessian1 decode with type: date', function() {
137-
hessian.decode(dateBuf1, '1.0', true);
138-
})
139-
.add('hessian2 decode with type: date', function() {
140-
hessian.decode(dateBuf2, '2.0', true);
141-
})
142-
143-
.add('hessian1 decode with type: long', function() {
144-
hessian.decode(longBuf1, '1.0', true);
145-
})
146-
.add('hessian2 decode with type: long', function() {
147-
hessian.decode(longBuf2, '2.0', true);
148-
})
149-
150-
.add('hessian1 decode with type: string', function() {
151-
hessian.decode(stringBuf1, '1.0', true);
152-
})
153-
.add('hessian2 decode with type: string', function() {
154-
hessian.decode(stringBuf2, '2.0', true);
155-
})
156-
157-
.add('hessian1 decode with type: [1, 2, 3]', function() {
158-
hessian.decode(arrBuf1, '1.0', true);
159-
})
160-
.add('hessian2 decode with type: [1, 2, 3]', function() {
161-
hessian.decode(arrBuf2, '2.0', true);
162-
})
163-
.add('hessian1 decode with type array', function() {
164-
hessian.decode(arrObjectBuf1, '1.0', true);
165-
})
166-
.add('hessian2 decode with type array', function() {
167-
hessian.decode(arrObjectBuf2, '2.0', true);
168-
})
169-
170-
.add('hessian1 decode with type: simple object', function() {
171-
hessian.decode(simpleObjectBuf1, '1.0', true);
172-
})
173-
.add('hessian2 decode with type: simple object', function() {
174-
hessian.decode(simpleObjectBuf2, '2.0', true);
175-
})
176-
177-
.add('hessian1 decode with type: complex object', function() {
178-
hessian.decode(complexObjectBuf1, '1.0', true);
179-
})
180-
.add('hessian2 decode with type: complex object', function() {
181-
hessian.decode(complexObjectBuf2, '2.0', true);
182-
})
74+
suite
75+
// .add('hessian1 decode: number', function() {
76+
// hessian.decode(number1Buf1, '1.0');
77+
// })
78+
.add('hessian2 decode: number', function() {
79+
hessian.decode(number1Buf2, '2.0');
80+
})
81+
// .add('hessian1 decode: date', function() {
82+
// hessian.decode(dateBuf1, '1.0');
83+
// })
84+
.add('hessian2 decode: date', function() {
85+
hessian.decode(dateBuf2, '2.0');
86+
})
87+
// .add('hessian1 decode: long', function() {
88+
// hessian.decode(longBuf1, '1.0');
89+
// })
90+
// .add('hessian2 decode: long', function() {
91+
// hessian.decode(longBuf2, '2.0');
92+
// })
93+
// .add('hessian1 decode: string', function() {
94+
// hessian.decode(stringBuf1, '1.0');
95+
// })
96+
.add('hessian2 decode: string', function() {
97+
hessian.decode(stringBuf2, '2.0');
98+
})
99+
100+
// .add('hessian1 decode: [1, 2, 3]', function() {
101+
// hessian.decode(arrBuf1, '1.0');
102+
// })
103+
// .add('hessian2 decode: [1, 2, 3]', function() {
104+
// hessian.decode(arrBuf2, '2.0');
105+
// })
106+
// .add('hessian1 decode array', function() {
107+
// hessian.decode(arrObjectBuf1, '1.0');
108+
// })
109+
// .add('hessian2 decode array', function() {
110+
// hessian.decode(arrObjectBuf2, '2.0');
111+
// })
112+
113+
// .add('hessian1 decode: simple object', function() {
114+
// hessian.decode(simpleObjectBuf1, '1.0');
115+
// })
116+
// .add('hessian2 decode: simple object', function() {
117+
// hessian.decode(simpleObjectBuf2, '2.0');
118+
// })
119+
120+
// .add('hessian1 decode: complex object', function() {
121+
// hessian.decode(complexObjectBuf1, '1.0');
122+
// })
123+
// .add('hessian2 decode: complex object', function() {
124+
// hessian.decode(complexObjectBuf2, '2.0');
125+
// })
126+
// .add('hessian1 decode with type: number', function() {
127+
// hessian.decode(number1Buf1, '1.0', true);
128+
// })
129+
// .add('hessian2 decode with type: number', function() {
130+
// hessian.decode(number1Buf2, '2.0', true);
131+
// })
132+
133+
// .add('hessian1 decode with type: date', function() {
134+
// hessian.decode(dateBuf1, '1.0', true);
135+
// })
136+
// .add('hessian2 decode with type: date', function() {
137+
// hessian.decode(dateBuf2, '2.0', true);
138+
// })
139+
140+
// .add('hessian1 decode with type: long', function() {
141+
// hessian.decode(longBuf1, '1.0', true);
142+
// })
143+
// .add('hessian2 decode with type: long', function() {
144+
// hessian.decode(longBuf2, '2.0', true);
145+
// })
146+
147+
// .add('hessian1 decode with type: string', function() {
148+
// hessian.decode(stringBuf1, '1.0', true);
149+
// })
150+
// .add('hessian2 decode with type: string', function() {
151+
// hessian.decode(stringBuf2, '2.0', true);
152+
// })
153+
154+
// .add('hessian1 decode with type: [1, 2, 3]', function() {
155+
// hessian.decode(arrBuf1, '1.0', true);
156+
// })
157+
// .add('hessian2 decode with type: [1, 2, 3]', function() {
158+
// hessian.decode(arrBuf2, '2.0', true);
159+
// })
160+
// .add('hessian1 decode with type array', function() {
161+
// hessian.decode(arrObjectBuf1, '1.0', true);
162+
// })
163+
// .add('hessian2 decode with type array', function() {
164+
// hessian.decode(arrObjectBuf2, '2.0', true);
165+
// })
166+
167+
// .add('hessian1 decode with type: simple object', function() {
168+
// hessian.decode(simpleObjectBuf1, '1.0', true);
169+
// })
170+
// .add('hessian2 decode with type: simple object', function() {
171+
// hessian.decode(simpleObjectBuf2, '2.0', true);
172+
// })
173+
174+
// .add('hessian1 decode with type: complex object', function() {
175+
// hessian.decode(complexObjectBuf1, '1.0', true);
176+
// })
177+
// .add('hessian2 decode with type: complex object', function() {
178+
// hessian.decode(complexObjectBuf2, '2.0', true);
179+
// })
183180

184181
.on('cycle', function(event) {
185-
benchmarks.add(event.target);
186-
})
187-
.on('start', function(event) {
188-
console.log('\n Hessian Decode Benchmark\n node version: %s, date: %s\n Starting...',
189-
process.version, Date());
190-
})
191-
.on('complete', function done() {
192-
benchmarks.log();
193-
})
194-
.run({ 'async': false });
195-
196-
// Hessian Decode Benchmark
197-
// node version: v0.11.12, date: Wed Jun 25 2014 10:46:26 GMT+0800 (CST)
198-
// Starting...
199-
// 32 tests completed.
200-
201-
// hessian1 decode: number x 5,983,374 ops/sec ±1.57% (94 runs sampled)
202-
// hessian2 decode: number x 5,713,562 ops/sec ±2.22% (91 runs sampled)
203-
// hessian1 decode: date x 2,959,698 ops/sec ±1.19% (92 runs sampled)
204-
// hessian2 decode: date x 2,548,345 ops/sec ±1.32% (91 runs sampled)
205-
// hessian1 decode: long x 3,880,734 ops/sec ±1.66% (93 runs sampled)
206-
// hessian2 decode: long x 5,221,659 ops/sec ±2.30% (90 runs sampled)
207-
// hessian1 decode: string x 1,109,890 ops/sec ±1.59% (94 runs sampled)
208-
// hessian2 decode: string x 1,075,246 ops/sec ±1.43% (92 runs sampled)
209-
// hessian1 decode: [1, 2, 3] x 1,186,054 ops/sec ±1.24% (92 runs sampled)
210-
// hessian2 decode: [1, 2, 3] x 2,049,867 ops/sec ±1.75% (98 runs sampled)
211-
// hessian1 decode array x 511,250 ops/sec ±0.92% (95 runs sampled)
212-
// hessian2 decode array x 600,564 ops/sec ±1.27% (90 runs sampled)
213-
// hessian1 decode: simple object x 186,875 ops/sec ±0.81% (93 runs sampled)
214-
// hessian2 decode: simple object x 174,768 ops/sec ±0.74% (94 runs sampled)
215-
// hessian1 decode: complex object x 133,573 ops/sec ±1.31% (97 runs sampled)
216-
// hessian2 decode: complex object x 131,234 ops/sec ±1.45% (95 runs sampled)
217-
// hessian1 decode with type: number x 5,014,602 ops/sec ±1.97% (91 runs sampled)
218-
// hessian2 decode with type: number x 5,890,098 ops/sec ±1.81% (89 runs sampled)
219-
// hessian1 decode with type: date x 2,797,789 ops/sec ±1.82% (91 runs sampled)
220-
// hessian2 decode with type: date x 2,541,107 ops/sec ±2.40% (92 runs sampled)
221-
// hessian1 decode with type: long x 3,869,288 ops/sec ±1.31% (93 runs sampled)
222-
// hessian2 decode with type: long x 5,598,654 ops/sec ±1.20% (95 runs sampled)
223-
// hessian1 decode with type: string x 1,068,879 ops/sec ±1.89% (92 runs sampled)
224-
// hessian2 decode with type: string x 1,110,680 ops/sec ±0.89% (94 runs sampled)
225-
// hessian1 decode with type: [1, 2, 3] x 1,150,027 ops/sec ±0.94% (94 runs sampled)
226-
// hessian2 decode with type: [1, 2, 3] x 1,834,472 ops/sec ±0.90% (94 runs sampled)
227-
// hessian1 decode with type array x 508,028 ops/sec ±0.99% (96 runs sampled)
228-
// hessian2 decode with type array x 605,446 ops/sec ±1.10% (96 runs sampled)
229-
// hessian1 decode with type: simple object x 190,014 ops/sec ±0.92% (97 runs sampled)
230-
// hessian2 decode with type: simple object x 176,337 ops/sec ±1.16% (96 runs sampled)
231-
// hessian1 decode with type: complex object x 133,815 ops/sec ±1.21% (90 runs sampled)
232-
// hessian2 decode with type: complex object x 134,814 ops/sec ±0.96% (96 runs sampled)
182+
benchmarks.add(event.target);
183+
})
184+
.on('start', function(event) {
185+
console.log('\n Hessian Decode Benchmark\n node version: %s, date: %s\n Starting...',
186+
process.version, Date());
187+
})
188+
.on('complete', function done() {
189+
benchmarks.log();
190+
})
191+
.run({ 'async': false });
192+
193+
// Hessian Decode Benchmark
194+
// node version: v0.11.12, date: Wed Jun 25 2014 10:46:26 GMT+0800 (CST)
195+
// Starting...
196+
// 32 tests completed.
197+
198+
// hessian1 decode: number x 5,983,374 ops/sec ±1.57% (94 runs sampled)
199+
// hessian2 decode: number x 5,713,562 ops/sec ±2.22% (91 runs sampled)
200+
// hessian1 decode: date x 2,959,698 ops/sec ±1.19% (92 runs sampled)
201+
// hessian2 decode: date x 2,548,345 ops/sec ±1.32% (91 runs sampled)
202+
// hessian1 decode: long x 3,880,734 ops/sec ±1.66% (93 runs sampled)
203+
// hessian2 decode: long x 5,221,659 ops/sec ±2.30% (90 runs sampled)
204+
// hessian1 decode: string x 1,109,890 ops/sec ±1.59% (94 runs sampled)
205+
// hessian2 decode: string x 1,075,246 ops/sec ±1.43% (92 runs sampled)
206+
// hessian1 decode: [1, 2, 3] x 1,186,054 ops/sec ±1.24% (92 runs sampled)
207+
// hessian2 decode: [1, 2, 3] x 2,049,867 ops/sec ±1.75% (98 runs sampled)
208+
// hessian1 decode array x 511,250 ops/sec ±0.92% (95 runs sampled)
209+
// hessian2 decode array x 600,564 ops/sec ±1.27% (90 runs sampled)
210+
// hessian1 decode: simple object x 186,875 ops/sec ±0.81% (93 runs sampled)
211+
// hessian2 decode: simple object x 174,768 ops/sec ±0.74% (94 runs sampled)
212+
// hessian1 decode: complex object x 133,573 ops/sec ±1.31% (97 runs sampled)
213+
// hessian2 decode: complex object x 131,234 ops/sec ±1.45% (95 runs sampled)
214+
// hessian1 decode with type: number x 5,014,602 ops/sec ±1.97% (91 runs sampled)
215+
// hessian2 decode with type: number x 5,890,098 ops/sec ±1.81% (89 runs sampled)
216+
// hessian1 decode with type: date x 2,797,789 ops/sec ±1.82% (91 runs sampled)
217+
// hessian2 decode with type: date x 2,541,107 ops/sec ±2.40% (92 runs sampled)
218+
// hessian1 decode with type: long x 3,869,288 ops/sec ±1.31% (93 runs sampled)
219+
// hessian2 decode with type: long x 5,598,654 ops/sec ±1.20% (95 runs sampled)
220+
// hessian1 decode with type: string x 1,068,879 ops/sec ±1.89% (92 runs sampled)
221+
// hessian2 decode with type: string x 1,110,680 ops/sec ±0.89% (94 runs sampled)
222+
// hessian1 decode with type: [1, 2, 3] x 1,150,027 ops/sec ±0.94% (94 runs sampled)
223+
// hessian2 decode with type: [1, 2, 3] x 1,834,472 ops/sec ±0.90% (94 runs sampled)
224+
// hessian1 decode with type array x 508,028 ops/sec ±0.99% (96 runs sampled)
225+
// hessian2 decode with type array x 605,446 ops/sec ±1.10% (96 runs sampled)
226+
// hessian1 decode with type: simple object x 190,014 ops/sec ±0.92% (97 runs sampled)
227+
// hessian2 decode with type: simple object x 176,337 ops/sec ±1.16% (96 runs sampled)
228+
// hessian1 decode with type: complex object x 133,815 ops/sec ±1.21% (90 runs sampled)
229+
// hessian2 decode with type: complex object x 134,814 ops/sec ±0.96% (96 runs sampled)

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ var DecoderV2 = exports.DecoderV2 = require('./lib/v2/decoder');
1818
exports.encoderV1 = new EncoderV1({size: 1024 * 1024});
1919
exports.encoderV2 = new EncoderV2({size: 1024 * 1024});
2020

21+
var decoderV1 = new DecoderV1();
22+
var decoderV2 = new DecoderV2();
23+
2124
exports.decode = function decode(buf, version, withType) {
2225
if (typeof version === 'boolean') {
2326
// buf, withType, version
@@ -29,9 +32,9 @@ exports.decode = function decode(buf, version, withType) {
2932
withType = !!withType;
3033

3134
if (version === '2.0') {
32-
return new DecoderV2(buf).read(withType);
35+
return decoderV2.init(buf).read(withType);
3336
}
34-
return new DecoderV1(buf).read(withType);
37+
return decoderV1.init(buf).read(withType);
3538
};
3639

3740
exports.encode = function encode(obj, version) {

0 commit comments

Comments
 (0)