From 252ea397055208436253a740aaf7aaae1edf5488 Mon Sep 17 00:00:00 2001 From: Ryan Lester Date: Tue, 4 May 2021 20:00:21 -0400 Subject: [PATCH] encode/decode minor performance optimization https://github.com/msgpack/msgpack-javascript/issues/172 --- src/decode.ts | 31 +++++++++++++------------------ src/encode.ts | 23 +++++++++++++---------- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/src/decode.ts b/src/decode.ts index 267b7d92..4d8300fb 100644 --- a/src/decode.ts +++ b/src/decode.ts @@ -40,7 +40,18 @@ export type DecodeOptions = Readonly< > & ContextOf; +const getDecoder = (options: any) => new Decoder( + options.extensionCodec, + (options as typeof options & { context: any }).context, + options.maxStrLength, + options.maxBinLength, + options.maxArrayLength, + options.maxMapLength, + options.maxExtLength, +); + export const defaultDecodeOptions: DecodeOptions = {}; +const defaultDecoder = getDecoder(defaultDecodeOptions); /** * It decodes a single MessagePack object in a buffer. @@ -52,15 +63,7 @@ export function decode( buffer: ArrayLike | BufferSource, options: DecodeOptions> = defaultDecodeOptions as any, ): unknown { - const decoder = new Decoder( - options.extensionCodec, - (options as typeof options & { context: any }).context, - options.maxStrLength, - options.maxBinLength, - options.maxArrayLength, - options.maxMapLength, - options.maxExtLength, - ); + const decoder = options === defaultDecodeOptions ? defaultDecoder : getDecoder(options); return decoder.decode(buffer); } @@ -72,14 +75,6 @@ export function decodeMulti( buffer: ArrayLike | BufferSource, options: DecodeOptions> = defaultDecodeOptions as any, ): Generator { - const decoder = new Decoder( - options.extensionCodec, - (options as typeof options & { context: any }).context, - options.maxStrLength, - options.maxBinLength, - options.maxArrayLength, - options.maxMapLength, - options.maxExtLength, - ); + const decoder = options === defaultDecodeOptions ? defaultDecoder : getDecoder(options); return decoder.decodeMulti(buffer); } diff --git a/src/encode.ts b/src/encode.ts index 07d59ad9..32ebbdac 100644 --- a/src/encode.ts +++ b/src/encode.ts @@ -55,7 +55,19 @@ export type EncodeOptions = Partial< > & ContextOf; +const getEncoder = (options: any) => new Encoder( + options.extensionCodec, + (options as typeof options & { context: any }).context, + options.maxDepth, + options.initialBufferSize, + options.sortKeys, + options.forceFloat32, + options.ignoreUndefined, + options.forceIntegerToFloat, +); + const defaultEncodeOptions: EncodeOptions = {}; +const defaultEncoder = getEncoder(defaultEncodeOptions); /** * It encodes `value` in the MessagePack format and @@ -67,15 +79,6 @@ export function encode( value: unknown, options: EncodeOptions> = defaultEncodeOptions as any, ): Uint8Array { - const encoder = new Encoder( - options.extensionCodec, - (options as typeof options & { context: any }).context, - options.maxDepth, - options.initialBufferSize, - options.sortKeys, - options.forceFloat32, - options.ignoreUndefined, - options.forceIntegerToFloat, - ); + const encoder = options === defaultEncodeOptions ? defaultEncoder : getEncoder(options); return encoder.encode(value); }