From b27aa8df44edef7992043823540b8755938a7718 Mon Sep 17 00:00:00 2001 From: Jonah Snider Date: Fri, 1 Nov 2024 00:12:01 -0700 Subject: [PATCH] perf: use closures instead of bound functions --- src/converters/convert.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/converters/convert.ts b/src/converters/convert.ts index 8405d74f..c4c78ed1 100644 --- a/src/converters/convert.ts +++ b/src/converters/convert.ts @@ -99,30 +99,28 @@ function convertToBest( }; } -type ConverterThis = { - _quantity: Q; - _from: U; -}; - function convertToAny( - this: ConverterThis, + quantity: Q, + from: U, to: MeasuresByUnit, ): LiteralToPrimitive; function convertToAny( - this: ConverterThis, + quantity: Q, + from: U, to: 'best', kind?: K | undefined, ): BestConversion>; function convertToAny( - this: ConverterThis, + quantity: Q, + from: U, to: MeasuresByUnit | 'best', kind?: K | undefined, ): LiteralToPrimitive | BestConversion> { if (to === 'best') { - return convertToBest(this._quantity, this._from, kind ?? 'metric'); + return convertToBest(quantity, from, kind ?? 'metric'); } - return convertTo(this._from, this._quantity, to); + return convertTo(from, quantity, to); } /** @@ -143,7 +141,9 @@ export function convert( throw new RangeError(`${from} is not a valid unit`); } - return { - to: convertToAny.bind({ _quantity: quantity, _from: from }), + const converter = { + to: (to, kind) => convertToAny(quantity, from, to, kind), } as Converter>; + + return converter; }