Skip to content

Commit

Permalink
don't create redundant zero-byte buffer on write; mark buf as optiona…
Browse files Browse the repository at this point in the history
…l in typings
  • Loading branch information
mourner committed Jul 8, 2024
1 parent 6dcee27 commit 2e67705
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ const PBF_FIXED32 = 5; // 32-bit: float, fixed32, sfixed32

export default class Pbf {
/**
* @param {Uint8Array | ArrayBuffer} buf
* @param {Uint8Array | ArrayBuffer} [buf]
*/
constructor(buf) {
this.buf = ArrayBuffer.isView && ArrayBuffer.isView(buf) ? buf : new Uint8Array(buf || 0);
constructor(buf = new Uint8Array(16)) {
this.buf = ArrayBuffer.isView(buf) ? buf : new Uint8Array(buf);
this.dataView = new DataView(this.buf.buffer);
this.pos = 0;
this.type = 0;
Expand Down
5 changes: 3 additions & 2 deletions test/pbf.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,9 @@ test('readPacked and writePacked', () => {
});

test('writePacked skips empty arrays', () => {
const buf = new Pbf();
buf.writePackedBoolean(1, []);
const pbf = new Pbf();
pbf.writePackedBoolean(1, []);
const buf = pbf.finish();
assert.equal(buf.length, 0);
});

Expand Down

0 comments on commit 2e67705

Please sign in to comment.