From 3d503666cafe5a923aeace840ba625fcc9f054fd Mon Sep 17 00:00:00 2001 From: Sergey Zenchenko Date: Mon, 27 May 2019 15:35:39 +0300 Subject: [PATCH] Improve array decoding performance --- src/Decoder.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Decoder.ts b/src/Decoder.ts index 84c4caa2..8cfb3879 100644 --- a/src/Decoder.ts +++ b/src/Decoder.ts @@ -23,6 +23,7 @@ type StackArrayState = { type: State.ARRAY; size: number; array: Array; + position: number; }; type StackState = StackArrayState | StackMapState; @@ -318,8 +319,9 @@ export class Decoder { // arrays and maps const state = stack[stack.length - 1]; if (state.type === State.ARRAY) { - state.array.push(object); - if (state.array.length === state.size) { + state.array[state.position] = object; + state.position++; + if (state.position === state.size) { stack.pop(); object = state.array; } else { @@ -387,7 +389,8 @@ export class Decoder { this.stack.push({ type: State.ARRAY, size, - array: [], + array: new Array(size), + position: 0, }); }