8000 Optimize string writing, too · protobufjs/bytebuffer.js@19e12ac · GitHub
[go: up one dir, main page]

Skip to content

Commit 19e12ac

Browse files
committed
Optimize string writing, too
1 parent d6c7d32 commit 19e12ac

File tree

10 files changed

+25
-41
lines changed

10 files changed

+25
-41
lines changed

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": "3.2.2",
3+
"version": "3.2.3",
44
"author": "Daniel Wirtz <dcode@dcode.io>",
55
"description": "A full-featured ByteBuffer implementation using typed arrays.",
66
"main": "dist/ByteBufferAB.js",

dist/ByteBufferAB.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
* @const
119119
* @expose
120120
*/
121-
ByteBuffer.VERSION = "3.2.2";
121+
ByteBuffer.VERSION = "3.2.3";
122122

123123
/**
124124
* Little endian constant that can be used instead of its boolean value. Evaluates to `true`.

dist/ByteBufferAB.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ByteBufferAB.min.js.gz

0 Bytes
Binary file not shown.

dist/ByteBufferNB.js

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ module.exports = (function() {
110110
* @const
111111
* @expose
112112
*/
113-
ByteBuffer.VERSION = "3.2.2";
113+
ByteBuffer.VERSION = "3.2.3";
114114

115115
/**
116116
* Little endian constant that can be used instead of its boolean value. Evaluates to `true`.
@@ -1553,17 +1553,14 @@ module.exports = (function() {
15531553
}
15541554
var start = offset;
15551555
// UTF8 strings do not contain zero bytes in between except for the zero character, so:
1556-
var buffer = new Buffer(str, 'utf8');
1557-
k = buffer.length;
1556+
k = Buffer.byteLength(str, "utf8");
15581557
offset += k+1;
15591558
var capacity12 = this.buffer.length;
15601559
if (offset > capacity12)
15611560
this.resize((capacity12 *= 2) > offset ? capacity12 : offset);
15621561
offset -= k+1;
1563-
buffer.copy(this.buffer, offset);
1564-
offset += k;
1562+
offset += this.buffer.write(str, offset, k, "utf8");
15651563
this.buffer[offset++] = 0;
1566-
buffer = null;
15671564
if (relative) {
15681565
this.offset = offset < 6D40 span class=pl-c1>- start;
15691566
return this;
@@ -1635,8 +1632,7 @@ module.exports = (function() {
16351632
}
16361633
var start = offset,
16371634
k;
1638-
var buffer = new Buffer(str, "utf8");
1639-
k = buffer.length;
1635+
k = Buffer.byteLength(str, "utf8");
16401636
offset += 4+k;
16411637
var capacity13 = this.buffer.length;
16421638
if (offset > capacity13)
@@ -1654,8 +1650,7 @@ module.exports = (function() {
16541650
this.buffer[offset+3] = k & 0xFF;
16551651
}
16561652
offset += 4;
1657-
buffer.copy(this.buffer, offset);
1658-
offset += k;
1653+
offset += this.buffer.write(str, offset, k, "utf8");
16591654
if (relative) {
16601655
this.offset = offset;
16611656
return this;
@@ -1748,16 +1743,15 @@ module.exports = (function() {
17481743
throw new RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.length);
17491744
}
17501745
var k;
1751-
var buffer = new Buffer(str, 'utf8');
1752-
k = buffer.length;
1746+
k = Buffer.byteLength(str, "utf8");
17531747
offset += k;
17541748
var capacity14 = this.buffer.length;
17551749
if (offset > capacity14)
17561750
this.resize((capacity14 *= 2) > offset ? capacity14 : offset);
17571751
offset -= k;
1758-
buffer.copy(this.buffer, offset);
1752+
offset += this.buffer.write(str, offset, k, "utf8");
17591753
if (relative) {
1760-
this.offset += k;
1754+
this.offset += offset;
17611755
return this;
17621756
}
17631757
return k;
@@ -1910,17 +1904,15 @@ module.exports = (function() {
19101904
}
19111905
var start = offset,
19121906
k, l;
1913-
var buffer = new Buffer(str, "utf8");
1914-
k = buffer.length;
1907+
k = Buffer.byteLength(str, "utf8");
19151908
l = ByteBuffer.calculateVarint32(k);
19161909
offset += l+k;
19171910
var capacity15 = this.buffer.length;
19181911
if (offset > capacity15)
19191912
this.resize((capacity15 *= 2) > offset ? capacity15 : offset);
19201913
offset -= l+k;
1921-
offset += this.writeVarint32(buffer.length, offset);
1922-
buffer.copy(this.buffer, offset);
1923-
offset += buffer.length;
1914+
offset += this.writeVarint32(k, offset);
1915+
offset += this.buffer.write(str, offset, k, "utf8");
19241916
if (relative) {
19251917
this.offset = offset;
19261918
return this;

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": "3.2.2",
3+
"version": "3.2.3",
44
"author": "Daniel Wirtz <dcode@dcode.io>",
55
"description": "The swiss army knife for binary data in JavaScript.",
66
"main": "index.js",

src/types/strings/cstring.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,10 @@ ByteBuffer.prototype.writeCString = function(str, offset) {
2626
var start = offset;
2727
// UTF8 strings do not contain zero bytes in between except for the zero character, so:
2828
//? if (NODE) {
29-
var buffer = new Buffer(str, 'utf8');
30-
k = buffer.length;
29+
k = Buffer.byteLength(str, "utf8");
3130
//? ENSURE_CAPACITY('k+1');
32-
buffer.copy(this.buffer, offset);
33-
offset += k;
31+
offset += this.buffer.write(str, offset, k, "utf8");
3432
this.buffer[offset++] = 0;
35-
buffer = null;
3633
//? } else {
3734
k = utfx.calculateUTF16asUTF8(utfx.stringSource(str))[1];
3835
//? ENSURE_CAPACITY('k+1');

src/types/strings/istring.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ ByteBuffer.prototype.writeIString = function(str, offset) {
2020
var start = offset,
2121
k;
2222
//? if (NODE) {
23-
var buffer = new Buffer(str, "utf8");
24-
k = buffer.length;
23+
k = Buffer.byteLength(str, "utf8");
2524
//? ENSURE_CAPACITY('4+k');
2625
//? WRITE_UINT32_ARRAY('k');
2726
offset += 4;
28-
buffer.copy(this.buffer, offset);
29-
offset += k;
27+
offset += this.buffer.write(str, offset, k, "utf8");
3028
//? } else {
3129
k = utfx.calculateUTF16asUTF8(utfx.stringSource(str), this.noAssert)[1];
3230
//? ENSURE_CAPACITY('4+k');

src/types/strings/utf8string.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ ByteBuffer.prototype.writeUTF8String = function(str, offset) {
3131
}
3232
var k;
3333
//? if (NODE) {
34-
var buffer = new Buffer(str, 'utf8');
35-
k = buffer.length;
34+
k = Buffer.byteLength(str, "utf8");
3635
//? ENSURE_CAPACITY('k');
37-
buffer.copy(this.buffer, offset);
36+
offset += this.buffer.write(str, offset, k, "utf8");
3837
if (relative) {
39-
this.offset += k;
38+
this.offset += offset;
4039
return this;
4140
}
4241
return k;

src/types/strings/vstring.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ ByteBuffer.prototype.writeVString = function(str, offset) {
2020
var start = offset,
2121
k, l;
2222
//? if (NODE) {
23-
var buffer = new Buffer(str, "utf8");
24-
k = buffer.length;
23+
k = Buffer.byteLength(str, "utf8");
2524
l = ByteBuffer.calculateVarint32(k);
2625
//? ENSURE_CAPACITY('l+k');
27-
offset += this.writeVarint32(buffer.length, offset);
28-
buffer.copy(this.buffer, offset);
29-
offset += buffer.length;
26+
offset += this.writeVarint32(k, offset);
27+
offset += this.buffer.write(str, offset, k, "utf8");
3028
//? } else {
3129
k = utfx.calculateUTF16asUTF8(utfx.stringSource(str), this.noAssert)[1];
3230
l = ByteBuffer.calculateVarint32(k);

0 commit comments

Comments
 (0)
0