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 d338d94
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type DecodeOptions<ContextType = undefined> = Readonly<
ContextOf<ContextType>;

export const defaultDecodeOptions: DecodeOptions = {};
const defaultDecoder = new Decoder(defaultDecodeOptions as any);

/**
* It decodes a single MessagePack object in a buffer.
Expand All @@ -47,7 +48,7 @@ export function decode<ContextType = undefined>(
buffer: ArrayLike<number> | BufferSource,
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any,
): unknown {
const decoder = new Decoder(
const decoder = options === defaultDecodeOptions ? defaultDecoder : new Decoder(
options.extensionCodec,
(options as typeof options & { context: any }).context,
options.maxStrLength,
Expand Down
3 changes: 2 additions & 1 deletion src/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type EncodeOptions<ContextType = undefined> = Partial<
ContextOf<ContextType>;

const defaultEncodeOptions: EncodeOptions = {};
const defaultEncoder = new Encoder(defaultEncodeOptions as any);

/**
* It encodes `value` in the MessagePack format and
Expand All @@ -47,7 +48,7 @@ export function encode<ContextType = undefined>(
value: unknown,
options: EncodeOptions<SplitUndefined<ContextType>> = defaultEncodeOptions as any,
): Uint8Array {
const encoder = new Encoder(
const encoder = options === defaultEncodeOptions ? defaultEncoder : new Encoder(
options.extensionCodec,
(options as typeof options & { context: any }).context,
options.maxDepth,
Expand Down

0 comments on commit d338d94

Please sign in to comment.