Skip to content

Commit 23310da

Browse files
committed
Prevent redundant DataView constructions
1 parent ff1fce6 commit 23310da

File tree

6 files changed

+83
-83
lines changed

6 files changed

+83
-83
lines changed

ByteBuffer.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
* @expose
6868
*/
6969
this.view = this.array != null ? new DataView(this.array) : null;
70-
7170
/**
7271
* Current read/write offset. Length- and capacity-independent index. Contents are the bytes between offset and
7372
* length, which are both absolute indexes. There is no capacity property, use {@link ByteBuffer#capacity}
@@ -1024,7 +1023,7 @@
10241023
// ref: http://code.google.com/searchframe#WTeibokF6gE/trunk/src/google/protobuf/io/coded_stream.cc
10251024
value = value >>> 0;
10261025
this.ensureCapacity(offset+ByteBuffer.calculateVarint32(value));
1027-
var dst = new DataView(this.array),
1026+
var dst = this.view,
10281027
size = 0;
10291028
dst.setUint8(offset, value | 0x80);
10301029
if (value >= (1 << 7)) {
@@ -1072,12 +1071,11 @@
10721071
offset = typeof offset !== 'undefined' ? offset : this.offset;
10731072
// ref: src/google/protobuf/io/coded_stream.cc
10741073

1075-
var src = new DataView(this.array),
1076-
count = 0,
1074+
var count = 0,
10771075
b;
10781076
var value = 0 >>> 0;
10791077
do {
1080-
b = src.getUint8(offset+count);
1078+
b = this.view.getUint8(offset+count);
10811079
if (count < ByteBuffer.MAX_VARINT32_BYTES) {
10821080
value |= ((b&0x7F)<<(7*count)) >>> 0;
10831081
}
@@ -1180,7 +1178,7 @@
11801178
size = ByteBuffer.calculateVarint64(value);
11811179

11821180
this.ensureCapacity(offset+size);
1183-
var dst = new DataView(this.array);
1181+
var dst = this.view;
11841182
switch (size) {
11851183
case 10: dst.setUint8(offset+9, (part2 >>> 7) | 0x80);
11861184
case 9 : dst.setUint8(offset+8, (part2 ) | 0x80);
@@ -1218,8 +1216,8 @@
12181216
var start = offset;
12191217
// ref: src/google/protobuf/io/coded_stream.cc
12201218

1221-
var src = new DataView(this.array);
1222-
var part0, part1 = 0, part2 = 0, b;
1219+
var src = this.view,
1220+
part0, part1 = 0, part2 = 0, b;
12231221
b = src.getUint8(offset++); part0 = (b & 0x7F) ; if (b & 0x80) {
12241222
b = src.getUint8(offset++); part0 |= (b & 0x7F) << 7; if (b & 0x80) {
12251223
b = src.getUint8(offset++); part0 |= (b & 0x7F) << 14; if (b & 0x80) {
@@ -1953,7 +1951,9 @@
19531951
asArray = !!asArray;
19541952
wrap = typeof wrap !== 'undefined' ? parseInt(wrap, 10) : 16;
19551953
if (wrap < 1) wrap = 16;
1956-
var out = "", lines = [], view = new DataView(this.array);
1954+
var out = "",
1955+
lines = [],
1956+
view = this.view;
19571957
if (this.offset == 0 && this.length == 0) {
19581958
out += "|";
19591959
} else if (this.length == 0) {
@@ -2001,13 +2001,13 @@
20012001
asArray = !!asArray;
20022002
wrap = typeof wrap !== 'undefined' ? parseInt(wrap, 10) : 16;
20032003
if (wrap < 1) wrap = 16;
2004-
var out = "", lines = [], view = new DataView(this.array);
2004+
var out = "", lines = [];
20052005
for (var i=0; i<this.array.byteLength; i++) {
20062006
if (i>0 && i%wrap == 0) {
20072007
lines.push(out);
20082008
out = "";
20092009
}
2010-
var val = view.getUint8(i);
2010+
var val = this.view.getUint8(i);
20112011
if (val > 32 && val < 127) {
20122012
val = String.fromCharCode(val);
20132013
} else {

0 commit comments

Comments
 (0)