Skip to content

Commit

Permalink
encode/decode minor performance optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
buu700 committed May 5, 2021
1 parent c66d264 commit 381f7e0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
31 changes: 13 additions & 18 deletions src/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,18 @@ export type DecodeOptions<ContextType = undefined> = Readonly<
> &
ContextOf<ContextType>;

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.
Expand All @@ -47,15 +58,7 @@ export function decode<ContextType = undefined>(
buffer: ArrayLike<number> | BufferSource,
options: DecodeOptions<SplitUndefined<ContextType>> = 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);
}

Expand All @@ -67,14 +70,6 @@ export function decodeMulti<ContextType = undefined>(
buffer: ArrayLike<number> | BufferSource,
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any,
): Generator<unknown, void, 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.decodeMulti(buffer);
}
23 changes: 13 additions & 10 deletions src/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,19 @@ export type EncodeOptions<ContextType = undefined> = Partial<
> &
ContextOf<ContextType>;

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
Expand All @@ -47,15 +59,6 @@ export function encode<ContextType = undefined>(
value: unknown,
options: EncodeOptions<SplitUndefined<ContextType>> = 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);
}

0 comments on commit 381f7e0

Please sign in to comment.