Skip to content

Commit 77c5da4

Browse files
committed
27: remove auto methods to get better perf in density calc
1 parent 093f2f5 commit 77c5da4

File tree

2 files changed

+62
-20
lines changed

2 files changed

+62
-20
lines changed

index.js

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@ module.exports = class BufferReader {
55
assert(Buffer.isBuffer(buffer), 'Invalid buffer.');
66
this.buffer = buffer;
77
this.offset = 0;
8-
this._makeNextReadFor('Int8', 1);
9-
this._makeNextReadFor('UInt8', 1);
10-
this._makeNextReadLEBEFor('UInt16', 2);
11-
this._makeNextReadLEBEFor('Int16', 2);
12-
this._makeNextReadLEBEFor('UInt32', 4);
13-
this._makeNextReadLEBEFor('Int32', 4);
14-
this._makeNextReadLEBEFor('Float', 4);
15-
this._makeNextReadLEBEFor('Double', 8);
168
}
179

1810
seek(offset, fromBeginning = true) {
@@ -24,6 +16,62 @@ module.exports = class BufferReader {
2416
this.offset = offset;
2517
}
2618

19+
nextDoubleLE() {
20+
return this._nextXX('DoubleLE', 8);
21+
}
22+
23+
nextDoubleBE() {
24+
return this._nextXX('DoubleBE', 8);
25+
}
26+
27+
nextFloatLE() {
28+
return this._nextXX('FloatLE', 4);
29+
}
30+
31+
nextFloatBE() {
32+
return this._nextXX('FloatBE', 4);
33+
}
34+
35+
nextInt32LE() {
36+
return this._nextXX('Int32LE', 4);
37+
}
38+
39+
nextInt32BE() {
40+
return this._nextXX('Int32BE', 4);
41+
}
42+
43+
nextUInt32LE() {
44+
return this._nextXX('UInt32LE', 4);
45+
}
46+
47+
nextUInt32BE() {
48+
return this._nextXX('UInt32BE', 4);
49+
}
50+
51+
nextUInt16LE() {
52+
return this._nextXX('UInt16LE', 2);
53+
}
54+
55+
nextUInt16BE() {
56+
return this._nextXX('UInt16BE', 2);
57+
}
58+
59+
nextInt16LE() {
60+
return this._nextXX('Int16LE', 2);
61+
}
62+
63+
nextInt16BE() {
64+
return this._nextXX('Int16BE', 2);
65+
}
66+
67+
nextUInt8() {
68+
return this._nextXX('UInt8', 1);
69+
}
70+
71+
nextInt8() {
72+
return this._nextXX('Int8', 1);
73+
}
74+
2775
nextBuffer(length) {
2876
this._checkPositive(length);
2977
this._checkOffsetInRange(this.offset + length);
@@ -42,18 +90,12 @@ module.exports = class BufferReader {
4290
return str;
4391
}
4492

45-
_makeNextReadLEBEFor(method, size) {
46-
this._makeNextReadFor(method + 'LE', size);
47-
this._makeNextReadFor(method + 'BE', size);
48-
}
93+
_nextXX(type, size) {
94+
this._checkOffsetInRange(this.offset + size, 'Offset');
4995

50-
_makeNextReadFor(method, size) {
51-
BufferReader.prototype[`next${method}`] = function() {
52-
this._checkOffsetInRange(this.offset + size, 'Offset');
53-
const val = this.buffer[`read${method}`](this.offset);
54-
this.offset += size;
55-
return val;
56-
};
96+
const v = this.buffer[`read${type}`](this.offset);
97+
this.offset += size;
98+
return v;
5799
}
58100

59101
_checkPositive(number, name = 'Length') {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ginkgoch-buffer-reader",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"main": "index.js",
55
"homepage": "https://github.com/ginkgoch/node-buffer-reader",
66
"scripts": {

0 commit comments

Comments
 (0)