Skip to content

Commit 708afa4

Browse files
committed
Minor performance improvements
1 parent 61a7026 commit 708afa4

File tree

9 files changed

+78
-61
lines changed

9 files changed

+78
-61
lines changed

ByteBuffer.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@
5050
* @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
5151
* @param {boolean=} littleEndian `true` to use little endian multi byte values, defaults to `false` for big
5252
* endian.
53+
* @param {boolean=} sparse If set to `true`, a ByteBuffer with array=view=null will be created which have to be
54+
* set manually afterwards. Defaults to `false`.
5355
* @expose
5456
*/
55-
var ByteBuffer = function(capacity, littleEndian) {
56-
57+
var ByteBuffer = function(capacity, littleEndian, sparse) {
5758
capacity = typeof capacity !== 'undefined' ? parseInt(capacity, 10) : ByteBuffer.DEFAULT_CAPACITY;
5859
if (capacity < 1) capacity = ByteBuffer.DEFAULT_CAPACITY;
5960

@@ -62,14 +63,14 @@
6263
* @type {?ArrayBuffer}
6364
* @expose
6465
*/
65-
this.array = arguments.length == 3 && arguments[2] === true ? null : new ArrayBuffer(capacity);
66+
this.array = sparse ? null : new ArrayBuffer(capacity);
6667

6768
/**
6869
* DataView to mess with the ArrayBuffer.
6970
* @type {?DataView}
7071
* @expose
7172
*/
72-
this.view = this.array != null ? new DataView(this.array) : null;
73+
this.view = sparse ? null : new DataView(this.array);
7374

7475
/**
7576
* Current read/write offset. Length- and capacity-independent index. Contents are the bytes between offset
@@ -110,7 +111,7 @@
110111
* @const
111112
* @expose
112113
*/
113-
ByteBuffer.VERSION = "2.3.0";
114+
ByteBuffer.VERSION = "2.3.1";
114115

115116
/**
116117
* Default buffer capacity of `16`. The ByteBuffer will be automatically resized by a factor of 2 if required.
@@ -243,7 +244,7 @@
243244
if (!(buffer instanceof ArrayBuffer)) {
244245
throw(new Error("Cannot wrap buffer of type "+typeof(buffer)+", "+buffer.constructor.name));
245246
}
246-
b = new ByteBuffer(0, littleEndian, /* shadow copy */ true);
247+
b = new ByteBuffer(0, littleEndian, true);
247248
b.array = buffer;
248249
b.view = b.array.byteLength > 0 ? new DataView(b.array) : null;
249250
b.offset = 0;
@@ -1781,7 +1782,7 @@
17811782
};
17821783

17831784
/**
1784-
* Decodes a binary string to a ByteBuffer.A binary string in this case is a string composed of 8bit values
1785+
* Decodes a binary string to a ByteBuffer. A binary string in this case is a string composed of 8bit values
17851786
* as characters with a char code between 0 and 255 inclusive.
17861787
* @param {string} str Binary string
17871788
* @param {boolean=} littleEndian `true` to use little endian byte order, defaults to `false` for big endian.
@@ -1801,7 +1802,11 @@
18011802
if ((val = str.charCodeAt(i)) > 255) throw(new Error("Illegal argument: Not a binary string (char code "+val+")"));
18021803
view.setUint8(i, val);
18031804
}
1804-
return ByteBuffer.wrap(dst, littleEndian);
1805+
var bb = new ByteBuffer(k, littleEndian, true);
1806+
bb.array = dst;
1807+
bb.view = view;
1808+
bb.length = k;
1809+
return bb;
18051810
};
18061811

18071812
/**

ByteBuffer.min.js

Lines changed: 32 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ByteBuffer.min.map

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

ByteBuffer.noexpose.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,24 @@
5050
* @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
5151
* @param {boolean=} littleEndian `true` to use little endian multi byte values, defaults to `false` for big
5252
* endian.
53+
* @param {boolean=} sparse If set to `true`, a ByteBuffer with array=view=null will be created which have to be
54+
* set manually afterwards. Defaults to `false`.
5355
*/
54-
var ByteBuffer = function(capacity, littleEndian) {
55-
56+
var ByteBuffer = function(capacity, littleEndian, sparse) {
5657
capacity = typeof capacity !== 'undefined' ? parseInt(capacity, 10) : ByteBuffer.DEFAULT_CAPACITY;
5758
if (capacity < 1) capacity = ByteBuffer.DEFAULT_CAPACITY;
5859

5960
/**
6061
* Backing ArrayBuffer.
6162
* @type {?ArrayBuffer}
6263
*/
63-
this.array = arguments.length == 3 && arguments[2] === true ? null : new ArrayBuffer(capacity);
64+
this.array = sparse ? null : new ArrayBuffer(capacity);
6465

6566
/**
6667
* DataView to mess with the ArrayBuffer.
6768
* @type {?DataView}
6869
*/
69-
this.view = this.array != null ? new DataView(this.array) : null;
70+
this.view = sparse ? null : new DataView(this.array);
7071

7172
/**
7273
* Current read/write offset. Length- and capacity-independent index. Contents are the bytes between offset
@@ -102,7 +103,7 @@
102103
* @type {string}
103104
* @const
104105
*/
105-
ByteBuffer.VERSION = "2.3.0";
106+
ByteBuffer.VERSION = "2.3.1";
106107

107108
/**
108109
* Default buffer capacity of `16`. The ByteBuffer will be automatically resized by a factor of 2 if required.
@@ -228,7 +229,7 @@
228229
if (!(buffer instanceof ArrayBuffer)) {
229230
throw(new Error("Cannot wrap buffer of type "+typeof(buffer)+", "+buffer.constructor.name));
230231
}
231-
b = new ByteBuffer(0, littleEndian, /* shadow copy */ true);
232+
b = new ByteBuffer(0, littleEndian, true);
232233
b.array = buffer;
233234
b.view = b.array.byteLength > 0 ? new DataView(b.array) : null;
234235
b.offset = 0;
@@ -1689,7 +1690,7 @@
16891690
};
16901691

16911692
/**
1692-
* Decodes a binary string to a ByteBuffer.A binary string in this case is a string composed of 8bit values
1693+
* Decodes a binary string to a ByteBuffer. A binary string in this case is a string composed of 8bit values
16931694
* as characters with a char code between 0 and 255 inclusive.
16941695
* @param {string} str Binary string
16951696
* @param {boolean=} littleEndian `true` to use little endian byte order, defaults to `false` for big endian.
@@ -1708,7 +1709,11 @@
17081709
if ((val = str.charCodeAt(i)) > 255) throw(new Error("Illegal argument: Not a binary string (char code "+val+")"));
17091710
view.setUint8(i, val);
17101711
}
1711-
return ByteBuffer.wrap(dst, littleEndian);
1712+
var bb = new ByteBuffer(k, littleEndian, true);
1713+
bb.array = dst;
1714+
bb.view = view;
1715+
bb.length = k;
1716+
return bb;
17121717
};
17131718

17141719
/**

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bytebuffer",
3-
"version": "2.3.0",
3+
"version": "2.3.1",
44
"author": "Daniel Wirtz <[email protected]>",
55
"description": "A full-featured ByteBuffer implementation using typed arrays.",
66
"main": "ByteBuffer.js",

docs/module-ByteBuffer.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1587,7 +1587,7 @@ <h4 class="name" id="decodeBinary"><span class="type-signature">&lt;static> </sp
15871587

15881588

15891589
<div class="description">
1590-
<p>Decodes a binary string to a ByteBuffer.A binary string in this case is a string composed of 8bit values
1590+
<p>Decodes a binary string to a ByteBuffer. A binary string in this case is a string composed of 8bit values
15911591
as characters with a char code between 0 and 255 inclusive.</p>
15921592
</div>
15931593

externs/ByteBuffer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@
2929
/**
3030
* @param {number=} capacity
3131
* @param {boolean=} littleEndian
32+
* @param {boolean=} sparse
3233
* @constructor
3334
*/
34-
function ByteBuffer(capacity, littleEndian) {};
35+
function ByteBuffer(capacity, littleEndian, sparse) {};
3536

3637
/**
3738
* @type {?ArrayBuffer}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bytebuffer",
3-
"version": "2.3.0",
3+
"version": "2.3.1",
44
"author": "Daniel Wirtz <[email protected]>",
55
"description": "A full-featured ByteBuffer implementation using typed arrays.",
66
"main": "ByteBuffer.js",

src/ByteBuffer.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@
5050
* @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
5151
* @param {boolean=} littleEndian `true` to use little endian multi byte values, defaults to `false` for big
5252
* endian.
53+
* @param {boolean=} sparse If set to `true`, a ByteBuffer with array=view=null will be created which have to be
54+
* set manually afterwards. Defaults to `false`.
5355
* @expose
5456
*/
55-
var ByteBuffer = function(capacity, littleEndian) {
56-
57+
var ByteBuffer = function(capacity, littleEndian, sparse) {
5758
capacity = typeof capacity !== 'undefined' ? parseInt(capacity, 10) : ByteBuffer.DEFAULT_CAPACITY;
5859
if (capacity < 1) capacity = ByteBuffer.DEFAULT_CAPACITY;
5960

@@ -62,14 +63,14 @@
6263
* @type {?ArrayBuffer}
6364
* @expose
6465
*/
65-
this.array = arguments.length == 3 && arguments[2] === true ? null : new ArrayBuffer(capacity);
66+
this.array = sparse ? null : new ArrayBuffer(capacity);
6667

6768
/**
6869
* DataView to mess with the ArrayBuffer.
6970
* @type {?DataView}
7071
* @expose
7172
*/
72-
this.view = this.array != null ? new DataView(this.array) : null;
73+
this.view = sparse ? null : new DataView(this.array);
7374

7475
/**
7576
* Current read/write offset. Length- and capacity-independent index. Contents are the bytes between offset
@@ -243,7 +244,7 @@
243244
if (!(buffer instanceof ArrayBuffer)) {
244245
throw(new Error("Cannot wrap buffer of type "+typeof(buffer)+", "+buffer.constructor.name));
245246
}
246-
b = new ByteBuffer(0, littleEndian, /* shadow copy */ true);
247+
b = new ByteBuffer(0, littleEndian, true);
247248
b.array = buffer;
248249
b.view = b.array.byteLength > 0 ? new DataView(b.array) : null;
249250
b.offset = 0;
@@ -1781,7 +1782,7 @@
17811782
};
17821783

17831784
/**
1784-
* Decodes a binary string to a ByteBuffer.A binary string in this case is a string composed of 8bit values
1785+
* Decodes a binary string to a ByteBuffer. A binary string in this case is a string composed of 8bit values
17851786
* as characters with a char code between 0 and 255 inclusive.
17861787
* @param {string} str Binary string
17871788
* @param {boolean=} littleEndian `true` to use little endian byte order, defaults to `false` for big endian.
@@ -1801,7 +1802,11 @@
18011802
if ((val = str.charCodeAt(i)) > 255) throw(new Error("Illegal argument: Not a binary string (char code "+val+")"));
18021803
view.setUint8(i, val);
18031804
}
1804-
return ByteBuffer.wrap(dst, littleEndian);
1805+
var bb = new ByteBuffer(k, littleEndian, true);
1806+
bb.array = dst;
1807+
bb.view = view;
1808+
bb.length = k;
1809+
return bb;
18051810
};
18061811

18071812
/**

0 commit comments

Comments
 (0)