Skip to content

Reuse Encoder/Decoder instance #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 13 additions & 18 deletions src/decode.ts
Original file line number Diff line number Diff line change
@@ -40,7 +40,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.
@@ -52,15 +63,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);
}

@@ -72,14 +75,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
@@ -55,7 +55,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
@@ -67,15 +79,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);
}