diff --git a/src/encoding/StringEncoder.ts b/src/encoding/StringEncoder.ts index 87ae890..6a9cb67 100644 --- a/src/encoding/StringEncoder.ts +++ b/src/encoding/StringEncoder.ts @@ -1,16 +1,9 @@ import Serializable from "./Serializable"; export default class StringEncoder implements Serializable { - // Default size of the decoder buffer that's always reused (in bytes) - private static readonly ENCODER_BUFFER_SIZE = 16384 - private textEncoder = new TextEncoder(); private textDecoder = new TextDecoder(); - private encoderBuffer: ArrayBuffer = new ArrayBuffer(StringEncoder.ENCODER_BUFFER_SIZE); - private encoderArray: Uint8Array = new Uint8Array(this.encoderBuffer); - private currentDecoderBufferSize: number = StringEncoder.ENCODER_BUFFER_SIZE; - decode(buffer: Uint8Array): string { return this.textDecoder.decode(buffer); } @@ -18,18 +11,8 @@ export default class StringEncoder implements Serializable { encode(stringValue: string, destination: Uint8Array): number { // Safari does not support the encodeInto function if (this.textEncoder.encodeInto !== undefined) { - const maxStringLength = stringValue.length * 3; - - if (this.currentDecoderBufferSize < maxStringLength) { - this.encoderBuffer = new ArrayBuffer(maxStringLength); - this.encoderArray = new Uint8Array(this.encoderBuffer); - this.currentDecoderBufferSize = maxStringLength; - } - - const writeResult = this.textEncoder.encodeInto(stringValue, this.encoderArray); - const writeLength = writeResult.written || 0; - destination.set(this.encoderArray.subarray(0, writeLength)); - return writeLength; + const writeResult = this.textEncoder.encodeInto(stringValue, destination); + return writeResult.written || 0; } else { const encodedString = this.textEncoder.encode(stringValue); destination.set(encodedString);