Skip to content

Commit 252ea39

Browse files
committed
encode/decode minor performance optimization
msgpack#172
1 parent c58b7e2 commit 252ea39

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
@@ -40,7 +40,18 @@ export type DecodeOptions<ContextType = undefined> = Readonly<
4040
> &
4141
ContextOf<ContextType>;
4242

43+
const getDecoder = (options: any) => new Decoder(
44+
options.extensionCodec,
45+
(options as typeof options & { context: any }).context,
46+
options.maxStrLength,
47+
options.maxBinLength,
48+
options.maxArrayLength,
49+
options.maxMapLength,
50+
options.maxExtLength,
51+
);
52+
4353
export const defaultDecodeOptions: DecodeOptions = {};
54+
const defaultDecoder = getDecoder(defaultDecodeOptions);
4455

4556
/**
4657
* It decodes a single MessagePack object in a buffer.
@@ -52,15 +63,7 @@ export function decode<ContextType = undefined>(
5263
buffer: ArrayLike<number> | BufferSource,
5364
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any,
5465
): unknown {
55-
const decoder = new Decoder(
56-
options.extensionCodec,
57-
(options as typeof options & { context: any }).context,
58-
options.maxStrLength,
59-
options.maxBinLength,
60-
options.maxArrayLength,
61-
options.maxMapLength,
62-
options.maxExtLength,
63-
);
66+
const decoder = options === defaultDecodeOptions ? defaultDecoder : getDecoder(options);
6467
return decoder.decode(buffer);
6568
}
6669

@@ -72,14 +75,6 @@ export function decodeMulti<ContextType = undefined>(
7275
buffer: ArrayLike<number> | BufferSource,
7376
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any,
7477
): Generator<unknown, void, unknown> {
75-
const decoder = new Decoder(
76-
options.extensionCodec,
77-
(options as typeof options & { context: any }).context,
78-
options.maxStrLength,
79-
options.maxBinLength,
80-
options.maxArrayLength,
81-
options.maxMapLength,
82-
options.maxExtLength,
83-
);
78+
const decoder = options === defaultDecodeOptions ? defaultDecoder : getDecoder(options);
8479
return decoder.decodeMulti(buffer);
8580
}

src/encode.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,19 @@ export type EncodeOptions<ContextType = undefined> = Partial<
5555
> &
5656
ContextOf<ContextType>;
5757

58+
const getEncoder = (options: any) => new Encoder(
59+
options.extensionCodec,
60+
(options as typeof options & { context: any }).context,
61+
options.maxDepth,
62+
options.initialBufferSize,
63+
options.sortKeys,
64+
options.forceFloat32,
65+
options.ignoreUndefined,
66+
options.forceIntegerToFloat,
67+
);
68+
5869
const defaultEncodeOptions: EncodeOptions = {};
70+
const defaultEncoder = getEncoder(defaultEncodeOptions);
5971

6072
/**
6173
* It encodes `value` in the MessagePack format and
@@ -67,15 +79,6 @@ export function encode<ContextType = undefined>(
6779
value: unknown,
6880
options: EncodeOptions<SplitUndefined<ContextType>> = defaultEncodeOptions as any,
6981
): Uint8Array {
70-
const encoder = new Encoder(
71-
options.extensionCodec,
72-
(options as typeof options & { context: any }).context,
73-
options.maxDepth,
74-
options.initialBufferSize,
75-
options.sortKeys,
76-
options.forceFloat32,
77-
options.ignoreUndefined,
78-
options.forceIntegerToFloat,
79-
);
82+
const encoder = options === defaultEncodeOptions ? defaultEncoder : getEncoder(options);
8083
return encoder.encode(value);
8184
}

0 commit comments

Comments
 (0)