bin-serde 1.6.3 → 1.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.ts CHANGED
@@ -7,6 +7,9 @@ const MAX_POOLED = 4096;
7
7
  let slab: Uint8Array = new Uint8Array(SLAB_SIZE);
8
8
  let slabOffset = 0;
9
9
 
10
+ const f32 = new Float32Array(1);
11
+ const f32u8 = new Uint8Array(f32.buffer);
12
+
10
13
  function allocFromSlab(size: number): Uint8Array {
11
14
  if (size > MAX_POOLED) {
12
15
  // Too large for pool, allocate directly
@@ -111,8 +114,11 @@ export class Writer {
111
114
 
112
115
  writeFloat(val: number) {
113
116
  this.ensureSize(4);
114
- this.view.setFloat32(this.pos, val, true);
115
- this.pos += 4;
117
+ f32[0] = val;
118
+ this.bytes[this.pos++] = f32u8[0]!;
119
+ this.bytes[this.pos++] = f32u8[1]!;
120
+ this.bytes[this.pos++] = f32u8[2]!;
121
+ this.bytes[this.pos++] = f32u8[3]!;
116
122
  return this;
117
123
  }
118
124
 
@@ -218,11 +224,10 @@ export class Writer {
218
224
  export class Reader {
219
225
  private pos = 0;
220
226
  private bytes: Uint8Array;
221
- private view: DataView;
227
+ private _view: DataView | null = null; // lazily allocated
222
228
 
223
229
  constructor(buf: Uint8Array) {
224
230
  this.bytes = buf;
225
- this.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
226
231
  }
227
232
 
228
233
  readUInt8() {
@@ -258,9 +263,11 @@ export class Reader {
258
263
  }
259
264
 
260
265
  readFloat() {
261
- const val = this.view.getFloat32(this.pos, true);
262
- this.pos += 4;
263
- return val;
266
+ f32u8[0] = this.bytes[this.pos++];
267
+ f32u8[1] = this.bytes[this.pos++];
268
+ f32u8[2] = this.bytes[this.pos++];
269
+ f32u8[3] = this.bytes[this.pos++];
270
+ return f32[0];
264
271
  }
265
272
 
266
273
  readBits(numBits: number) {
@@ -307,4 +314,11 @@ export class Reader {
307
314
  remaining() {
308
315
  return this.bytes.length - this.pos;
309
316
  }
317
+
318
+ private get view(): DataView {
319
+ if (!this._view) {
320
+ this._view = new DataView(this.bytes.buffer, this.bytes.byteOffset);
321
+ }
322
+ return this._view;
323
+ }
310
324
  }
package/lib/index.d.ts CHANGED
@@ -22,7 +22,7 @@ export declare class Writer {
22
22
  export declare class Reader {
23
23
  private pos;
24
24
  private bytes;
25
- private view;
25
+ private _view;
26
26
  constructor(buf: Uint8Array);
27
27
  readUInt8(): number;
28
28
  readUInt16(): number;
@@ -35,4 +35,5 @@ export declare class Reader {
35
35
  readStringUtf8(len?: number): string;
36
36
  readBuffer(numBytes: number): Uint8Array;
37
37
  remaining(): number;
38
+ private get view();
38
39
  }
package/lib/index.js CHANGED
@@ -4,6 +4,8 @@ const SLAB_SIZE = 8192;
4
4
  const MAX_POOLED = 4096;
5
5
  let slab = new Uint8Array(SLAB_SIZE);
6
6
  let slabOffset = 0;
7
+ const f32 = new Float32Array(1);
8
+ const f32u8 = new Uint8Array(f32.buffer);
7
9
  function allocFromSlab(size) {
8
10
  if (size > MAX_POOLED) {
9
11
  // Too large for pool, allocate directly
@@ -96,8 +98,11 @@ export class Writer {
96
98
  }
97
99
  writeFloat(val) {
98
100
  this.ensureSize(4);
99
- this.view.setFloat32(this.pos, val, true);
100
- this.pos += 4;
101
+ f32[0] = val;
102
+ this.bytes[this.pos++] = f32u8[0];
103
+ this.bytes[this.pos++] = f32u8[1];
104
+ this.bytes[this.pos++] = f32u8[2];
105
+ this.bytes[this.pos++] = f32u8[3];
101
106
  return this;
102
107
  }
103
108
  writeBits(bits) {
@@ -194,10 +199,9 @@ export class Writer {
194
199
  export class Reader {
195
200
  pos = 0;
196
201
  bytes;
197
- view;
202
+ _view = null; // lazily allocated
198
203
  constructor(buf) {
199
204
  this.bytes = buf;
200
- this.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
201
205
  }
202
206
  readUInt8() {
203
207
  return this.bytes[this.pos++];
@@ -227,9 +231,11 @@ export class Reader {
227
231
  return val % 2 === 0 ? val / 2 : -(val + 1) / 2;
228
232
  }
229
233
  readFloat() {
230
- const val = this.view.getFloat32(this.pos, true);
231
- this.pos += 4;
232
- return val;
234
+ f32u8[0] = this.bytes[this.pos++];
235
+ f32u8[1] = this.bytes[this.pos++];
236
+ f32u8[2] = this.bytes[this.pos++];
237
+ f32u8[3] = this.bytes[this.pos++];
238
+ return f32[0];
233
239
  }
234
240
  readBits(numBits) {
235
241
  const numBytes = Math.ceil(numBits / 8);
@@ -271,4 +277,10 @@ export class Reader {
271
277
  remaining() {
272
278
  return this.bytes.length - this.pos;
273
279
  }
280
+ get view() {
281
+ if (!this._view) {
282
+ this._view = new DataView(this.bytes.buffer, this.bytes.byteOffset);
283
+ }
284
+ return this._view;
285
+ }
274
286
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bin-serde",
3
- "version": "1.6.3",
3
+ "version": "1.6.5",
4
4
  "description": "A low level library for efficiently writing and reading binary data in javascript",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",