Skip to content

Commit cb18994

Browse files
committed
directly use index instead of listing all the keys of array
* when serializing to json 1 element by 1 element * to avoid consume more memory when new array
1 parent d41f1e0 commit cb18994

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/JsonStreamStringify.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ class JsonStreamStringify extends Readable {
189189
obj.isEmpty = !obj.unread.length;
190190
} else if (type === 'Array') {
191191
this.depth += 1;
192-
obj.unread = Array.from(Array(realValue.length).keys());
193-
obj.isEmpty = !obj.unread.length;
192+
obj.unread = realValue.length;
193+
obj.isEmpty = !obj.unread;
194194
} else if (type.startsWith('Readable')) {
195195
this.depth += 1;
196196
if (realValue._readableState.ended) {
@@ -272,7 +272,17 @@ class JsonStreamStringify extends Readable {
272272
}
273273

274274
processArray(current) {
275-
return this.processObject(current);
275+
const key = current.unread;
276+
if (!key) {
277+
this.removeFromStack(current);
278+
return;
279+
}
280+
const index = current.value.length - key;
281+
const value = current.value[index];
282+
/* eslint-disable no-param-reassign */
283+
current.unread -= 1;
284+
/* eslint-enable no-param-reassign */
285+
this.addToStack(value, false, index, current);
276286
}
277287

278288
processPrimitive(current) {

0 commit comments

Comments
 (0)