json-stream-lite 1.3.0 → 1.3.1

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.
@@ -22,6 +22,10 @@ export declare class ByteBuffer {
22
22
  protected buffer: number[];
23
23
  /** Optional async iterable input source */
24
24
  protected asyncIterable?: ByteStream;
25
+ /** Cached sync iterator */
26
+ private syncIterator?;
27
+ /** Cached async iterator */
28
+ private asyncIterator?;
25
29
  /**
26
30
  * Creates a new ByteBuffer instance.
27
31
  *
@@ -44,6 +44,10 @@ export class ByteBuffer {
44
44
  buffer = [];
45
45
  /** Optional async iterable input source */
46
46
  asyncIterable;
47
+ /** Cached sync iterator */
48
+ syncIterator;
49
+ /** Cached async iterator */
50
+ asyncIterator;
47
51
  /**
48
52
  * Creates a new ByteBuffer instance.
49
53
  *
@@ -67,10 +71,12 @@ export class ByteBuffer {
67
71
  if (!this.asyncIterable || !(Symbol.iterator in this.asyncIterable)) {
68
72
  return false;
69
73
  }
70
- const iterator = this.asyncIterable[Symbol.iterator]();
74
+ if (!this.syncIterator) {
75
+ this.syncIterator = this.asyncIterable[Symbol.iterator]();
76
+ }
71
77
  let processed = false;
72
78
  while (this.length < this.maxBufferSize || !processed) {
73
- const next = iterator.next();
79
+ const next = this.syncIterator.next();
74
80
  processed = true;
75
81
  if (next.done) {
76
82
  this.eof = true;
@@ -91,11 +97,13 @@ export class ByteBuffer {
91
97
  !(Symbol.asyncIterator in this.asyncIterable)) {
92
98
  return;
93
99
  }
94
- const iterator = this.asyncIterable[Symbol.asyncIterator]();
100
+ if (!this.asyncIterator) {
101
+ this.asyncIterator = this.asyncIterable[Symbol.asyncIterator]();
102
+ }
95
103
  let processed = false;
96
104
  while (this.length < this.maxBufferSize || !processed) {
97
105
  processed = true;
98
- const next = await iterator.next();
106
+ const next = await this.asyncIterator.next();
99
107
  if (next.done) {
100
108
  this.eof = true;
101
109
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-stream-lite",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "A lightweight, memory-efficient, zero-dependency streaming JSON parser and stringifier for JavaScript and TypeScript. It works in both Node.js and browser environments.",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",