Skip to content

Commit d338d94

Browse files
committed
encode/decode minor performance optimization
msgpack#172
1 parent c66d264 commit d338d94

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

src/decode.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export type DecodeOptions<ContextType = undefined> = Readonly<
3636
ContextOf<ContextType>;
3737

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

4041
/**
4142
* It decodes a single MessagePack object in a buffer.
@@ -47,7 +48,7 @@ export function decode<ContextType = undefined>(
4748
buffer: ArrayLike<number> | BufferSource,
4849
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any,
4950
): unknown {
50-
const decoder = new Decoder(
51+
const decoder = options === defaultDecodeOptions ? defaultDecoder : new Decoder(
5152
options.extensionCodec,
5253
(options as typeof options & { context: any }).context,
5354
options.maxStrLength,

src/encode.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export type EncodeOptions<ContextType = undefined> = Partial<
3636
ContextOf<ContextType>;
3737

3838
const defaultEncodeOptions: EncodeOptions = {};
39+
const defaultEncoder = new Encoder(defaultEncodeOptions as any);
3940

4041
/**
4142
* It encodes `value` in the MessagePack format and
@@ -47,7 +48,7 @@ export function encode<ContextType = undefined>(
4748
value: unknown,
4849
options: EncodeOptions<SplitUndefined<ContextType>> = defaultEncodeOptions as any,
4950
): Uint8Array {
50-
const encoder = new Encoder(
51+
const encoder = options === defaultEncodeOptions ? defaultEncoder : new Encoder(
5152
options.extensionCodec,
5253
(options as typeof options & { context: any }).context,
5354
options.maxDepth,

0 commit comments

Comments
 (0)