Skip to content

Commit 381f7e0

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

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

src/decode.ts

+13-18
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,18 @@ export type DecodeOptions<ContextType = undefined> = Readonly<
3535
> &
3636
ContextOf<ContextType>;
3737

38+
const getDecoder = (options: any) => new Decoder(
39+
options.extensionCodec,
40+
(options as typeof options & { context: any }).context,
41+
options.maxStrLength,
42+
options.maxBinLength,
43+
options.maxArrayLength,
44+
options.maxMapLength,
45+
options.maxExtLength,
46+
);
47+
3848
export const defaultDecodeOptions: DecodeOptions = {};
49+
const defaultDecoder = getDecoder(defaultDecodeOptions);
3950

4051
/**
4152
* It decodes a single MessagePack object in a buffer.
@@ -47,15 +58,7 @@ export function decode<ContextType = undefined>(
4758
buffer: ArrayLike<number> | BufferSource,
4859
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any,
4960
): unknown {
50-
const decoder = new Decoder(
51-
options.extensionCodec,
52-
(options as typeof options & { context: any }).context,
53-
options.maxStrLength,
54-
options.maxBinLength,
55-
options.maxArrayLength,
56-
options.maxMapLength,
57-
options.maxExtLength,
58-
);
61+
const decoder = options === defaultDecodeOptions ? defaultDecoder : getDecoder(options);
5962
return decoder.decode(buffer);
6063
}
6164

@@ -67,14 +70,6 @@ export function decodeMulti<ContextType = undefined>(
6770
buffer: ArrayLike<number> | BufferSource,
6871
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any,
6972
): Generator<unknown, void, unknown> {
70-
const decoder = new Decoder(
71-
options.extensionCodec,
72-
(options as typeof options & { context: any }).context,
73-
options.maxStrLength,
74-
options.maxBinLength,
75-
options.maxArrayLength,
76-
options.maxMapLength,
77-
options.maxExtLength,
78-
);
73+
const decoder = options === defaultDecodeOptions ? defaultDecoder : getDecoder(options);
7974
return decoder.decodeMulti(buffer);
8075
}

src/encode.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,19 @@ export type EncodeOptions<ContextType = undefined> = Partial<
3535
> &
3636
ContextOf<ContextType>;
3737

38+
const getEncoder = (options: any) => new Encoder(
39+
options.extensionCodec,
40+
(options as typeof options & { context: any }).context,
41+
options.maxDepth,
42+
options.initialBufferSize,
43+
options.sortKeys,
44+
options.forceFloat32,
45+
options.ignoreUndefined,
46+
options.forceIntegerToFloat,
47+
);
48+
3849
const defaultEncodeOptions: EncodeOptions = {};
50+
const defaultEncoder = getEncoder(defaultEncodeOptions);
3951

4052
/**
4153
* It encodes `value` in the MessagePack format and
@@ -47,15 +59,6 @@ export function encode<ContextType = undefined>(
4759
value: unknown,
4860
options: EncodeOptions<SplitUndefined<ContextType>> = defaultEncodeOptions as any,
4961
): Uint8Array {
50-
const encoder = new Encoder(
51-
options.extensionCodec,
52-
(options as typeof options & { context: any }).context,
53-
options.maxDepth,
54-
options.initialBufferSize,
55-
options.sortKeys,
56-
options.forceFloat32,
57-
options.ignoreUndefined,
58-
options.forceIntegerToFloat,
59-
);
62+
const encoder = options === defaultEncodeOptions ? defaultEncoder : getEncoder(options);
6063
return encoder.encode(value);
6164
}

0 commit comments

Comments
 (0)