diff --git a/packages/types/src/codec/AbstractArray.ts b/packages/types/src/codec/AbstractArray.ts index 19e8ea8145..42a0d1cb15 100644 --- a/packages/types/src/codec/AbstractArray.ts +++ b/packages/types/src/codec/AbstractArray.ts @@ -98,18 +98,26 @@ export abstract class AbstractArray extends Array implements * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information */ public toHuman (isExtended?: boolean): AnyJson { - return this.map((entry): AnyJson => - entry.toHuman(isExtended) - ); + const result = new Array(this.length); + + for (let i = 0; i < this.length; i++) { + result[i] = this[i].toHuman(isExtended); + } + + return result; } /** * @description Converts the Object to JSON, typically used for RPC transfers */ public toJSON (): AnyJson { - return this.map((entry): AnyJson => - entry.toJSON() - ); + const result = new Array(this.length); + + for (let i = 0; i < this.length; i++) { + result[i] = this[i].toJSON(); + } + + return result; } /** @@ -121,12 +129,13 @@ export abstract class AbstractArray extends Array implements * @description Returns the string representation of the value */ public override toString (): string { - // Overwrite the default toString representation of Array. - const data = this.map((entry): string => - entry.toString() - ); + const result = new Array(this.length); - return `[${data.join(', ')}]`; + for (let i = 0; i < this.length; i++) { + result[i] = this[i].toString(); + } + + return `[${result.join(', ')}]`; } /** @@ -134,9 +143,11 @@ export abstract class AbstractArray extends Array implements * @param isBare true when the value has none of the type-specific prefixes (internal) */ public toU8a (isBare?: boolean): Uint8Array { - const encoded = this.map((entry): Uint8Array => - entry.toU8a(isBare) - ); + const encoded = new Array(this.length); + + for (let i = 0; i < this.length; i++) { + encoded[i] = this[i].toU8a(isBare); + } return isBare ? u8aConcat(...encoded) diff --git a/packages/types/src/codec/Struct.ts b/packages/types/src/codec/Struct.ts index 2e997669c3..7941af1043 100644 --- a/packages/types/src/codec/Struct.ts +++ b/packages/types/src/codec/Struct.ts @@ -303,18 +303,22 @@ export class Struct< public toU8a (isBare?: BareOpts): Uint8Array { // we have keyof S here, cast to string to make it compatible with isBare const entries = [...this.entries()] as [string, Codec][]; + const encoded: Uint8Array[] = []; - return u8aConcat( - ...entries - // eslint-disable-next-line @typescript-eslint/unbound-method - .filter(([, value]) => isFunction(value?.toU8a)) - .map(([key, value]): Uint8Array => - value.toU8a( + for (let i = 0; i < entries.length; i++) { + const [k, v] = entries[i]; + + if (v && isFunction(v.toU8a)) { + encoded.push( + v.toU8a( !isBare || isBoolean(isBare) ? isBare - : isBare[key] + : isBare[k] ) - ) - ); + ); + } + } + + return u8aConcat(...encoded); } } diff --git a/packages/types/src/codec/Tuple.ts b/packages/types/src/codec/Tuple.ts index 3f7dfd5e4b..218feae081 100644 --- a/packages/types/src/codec/Tuple.ts +++ b/packages/types/src/codec/Tuple.ts @@ -122,10 +122,12 @@ export class Tuple extends AbstractArray implements ITuple { * @param isBare true when the value has none of the type-specific prefixes (internal) */ public override toU8a (isBare?: boolean): Uint8Array { - return u8aConcat( - ...this.map((e): Uint8Array => - e.toU8a(isBare) - ) - ); + const encoded = new Array(this.length); + + for (let i = 0; i < this.length; i++) { + encoded[i] = this[i].toU8a(isBare); + } + + return u8aConcat(...encoded); } } diff --git a/packages/types/src/codec/VecFixed.ts b/packages/types/src/codec/VecFixed.ts index 4e01f36af5..e3e199df9a 100644 --- a/packages/types/src/codec/VecFixed.ts +++ b/packages/types/src/codec/VecFixed.ts @@ -65,7 +65,11 @@ export class VecFixed extends AbstractArray { public override toU8a (): Uint8Array { // we override, we don't add the length prefix for ourselves, and at the same time we // ignore isBare on entries, since they should be properly encoded at all times - const encoded = this.map((e) => e.toU8a()); + const encoded = new Array(this.length); + + for (let i = 0; i < this.length; i++) { + encoded[i] = this[i].toU8a(); + } return encoded.length ? u8aConcat(...encoded)