Skip to content

Commit 2730b57

Browse files
committed
Minor optimizations
1 parent db6eba7 commit 2730b57

File tree

9 files changed

+165
-118
lines changed

9 files changed

+165
-118
lines changed

ByteBuffer.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
// uses DataView instead. This is required for IE 8 compatibility.
2828

2929
/**
30-
* @param {?Function} Long
30+
* @param {Function=} Long
3131
* @returns {Function}
3232
* @private
3333
*/
@@ -167,7 +167,7 @@
167167
function b2ab(b) {
168168
var ab = new ArrayBuffer(b.length),
169169
view = new Uint8Array(ab);
170-
for (var i = 0; i < b.length; ++i) view[i] = b[i];
170+
for (var i= 0, k=b.length; i < k; ++i) view[i] = b[i];
171171
return ab;
172172
}
173173

@@ -621,9 +621,10 @@
621621
* @expose
622622
*/
623623
ByteBuffer.prototype.writeUint8 = function(value, offset) {
624-
offset = typeof offset != 'undefined' ? offset : (this.offset+=1)-1;
624+
offset = typeof offset !== 'undefined' ? offset : (this.offset+=1)-1;
625625
this.ensureCapacity(offset+1);
626626
this.view.setUint8(offset, value);
627+
this.view.setUint8(offset, value);
627628
return this;
628629
};
629630

@@ -1581,7 +1582,7 @@
15811582
ByteBuffer.calculateUTF8String = function(str) {
15821583
str = ""+str;
15831584
var bytes = 0;
1584-
for (var i=0; i<str.length; i++) {
1585+
for (var i=0, k=str.length; i<k; i++) {
15851586
// Does not throw since JS strings are already UTF8 encoded
15861587
bytes += ByteBuffer.calculateUTF8Char(str.charCodeAt(i));
15871588
}
@@ -1685,7 +1686,7 @@
16851686
}
16861687
var o,
16871688
out = new ByteBuffer(str.length/2, littleEndian);
1688-
for (var i=0; i<str.length; i+=2) {
1689+
for (var i= 0, k=str.length; i<k; i+=2) {
16891690
out.writeUint8(parseInt(str.substring(i, i+2), 16));
16901691
}
16911692
return out.flip();
@@ -1703,9 +1704,9 @@
17031704
var advance = typeof offset === 'undefined';
17041705
offset = typeof offset !== 'undefined' ? offset : this.offset;
17051706
var start = offset;
1706-
var encLen = ByteBuffer.calculateUTF8String(str), i; // See [1]
1707+
var encLen = ByteBuffer.calculateUTF8String(str); // See [1]
17071708
this.ensureCapacity(offset+encLen);
1708-
for (i=0; i<str.length; i++) {
1709+
for (var i=0, j=str.length; i<j; i++) {
17091710
// [1] Does not throw since JS strings are already UTF8 encoded
17101711
offset += ByteBuffer.encodeUTF8Char(str.charCodeAt(i), this, offset);
17111712
}
@@ -1972,7 +1973,8 @@
19721973
// Left colum: hex with offsets
19731974
var out = "",
19741975
lines = [],
1975-
val;
1976+
val,
1977+
view = this.view;
19761978
if (this.offset == 0 && this.length == 0) {
19771979
out += "|";
19781980
} else if (this.length == 0) {
@@ -1982,13 +1984,13 @@
19821984
} else {
19831985
out += " ";
19841986
}
1985-
for (var i=0; i<this.array.byteLength; i++) {
1987+
for (var i=0, k=this.array.byteLength; i<k; i++) {
19861988
if (i>0 && i%wrap == 0) {
19871989
while (out.length < 3*wrap+1) out += " "; // Make it equal to maybe show something on the right
19881990
lines.push(out);
19891991
out = " ";
19901992
}
1991-
val = this.view.getUint8(i).toString(16).toUpperCase();
1993+
val = view.getUint8(i).toString(16).toUpperCase();
19921994
if (val.length < 2) val = "0"+val;
19931995
out += val;
19941996
if (i+1 == this.offset && i+1 == this.length) {
@@ -2005,19 +2007,19 @@
20052007
lines.push(out);
20062008
}
20072009
// Make it equal
2008-
for (i=0; i<lines.length; i++) {
2010+
for (i=0, k=lines.length; i<k; i++) {
20092011
while (lines[i].length < 3*wrap+1) lines[i] += " "; // Make it equal to maybe show something on the right
20102012
}
20112013

20122014
// Right column: ASCII, using dots for (usually) non-printable characters
20132015
var n = 0;
20142016
out = "";
2015-
for (i=0; i<this.array.byteLength; i++) {
2017+
for (i=0, k=this.array.byteLength; i<k; i++) {
20162018
if (i>0 && i%wrap == 0) {
20172019
lines[n] += " "+out;
20182020
out = ""; n++;
20192021
}
2020-
val = this.view.getUint8(i);
2022+
val = view.getUint8(i);
20212023
out += val > 32 && val < 127 ? String.fromCharCode(val) : ".";
20222024
}
20232025
if (out != "") {
@@ -2047,18 +2049,20 @@
20472049
* @expose
20482050
*/
20492051
ByteBuffer.prototype.toHex = function(debug) {
2050-
var out = "", val, i;
2052+
var out = "",
2053+
val,
2054+
view = this.view,
2055+
i, k;
20512056
if (!debug) {
20522057
if (this.array == null) return "";
2053-
for (i=this.offset; i<this.length; i++) {
2054-
val = this.view.getUint8(i).toString(16).toUpperCase();
2058+
for (i=this.offset, k=this.length; i<k; i++) {
2059+
val = view.getUint8(i).toString(16).toUpperCase();
20552060
if (val.length < 2) val = "0"+val;
20562061
out += val;
20572062
}
20582063
return out;
20592064
} else {
20602065
if (this.array == null) return "DESTROYED";
2061-
var lines = [];
20622066
if (this.offset == 0 && this.length == 0) {
20632067
out += "|";
20642068
} else if (this.length == 0) {
@@ -2068,11 +2072,11 @@
20682072
} else {
20692073
out += " ";
20702074
}
2071-
for (i=0; i<this.array.byteLength; i++) {
2072-
val = this.view.getUint8(i).toString(16).toUpperCase();
2075+
for (i=0, k=this.array.byteLength; i<k; i++) {
2076+
val = view.getUint8(i).toString(16).toUpperCase();
20732077
if (val.length < 2) val = "0"+val;
20742078
out += val;
2075-
if (i+1 == this.offset && i+1 == this.length) {
2079+
if (i+1 === this.offset && i+1 === this.length) {
20762080
out += "|";
20772081
} else if (i+1 == this.offset) {
20782082
out += "<";
@@ -2175,13 +2179,12 @@
21752179
}
21762180
return new Buffer(new Uint8Array(this.array).subarray(offset, length));
21772181
};
2182+
21782183
}
21792184

21802185
return ByteBuffer;
21812186
}
2182-
2183-
2184-
2187+
21852188
// Enable module loading if available
21862189
if (typeof module !== 'undefined' && module["exports"]) { // CommonJS
21872190
module["exports"] = loadByteBuffer(require("long"));

0 commit comments

Comments
 (0)