Skip to content

convertUnit

function convertUnit(
value,
from,
to): number;

Defined in: packages/engine/src/uom/UomConverter.ts:182

Convert value from unit from to unit to.

Does not validate that the conversion is possible, callers should check with canConvert first if from/to aren’t already known-good.

THROWS a RangeError for an unknown unit or a cross-measure pair. VM.ts’s extractDurationMs() depends on that, see convertRaw.

This used to memoize a per-pair conversion factor in an LFU cache. The cache is gone: computing the factor is now two Map reads, while the cache cost a template-string key allocation on every call plus the LFU’s own frequency bookkeeping, so it was slower than the work it avoided. Removing it is bit-exact, not merely close: the cache stored convertRaw(1, f, t), which is exactly fromRatio / toRatio, and then multiplied, which is the identical expression convertRaw(value, f, t) evaluates. It also deleted the special case that existed only to keep offset-based temperature pairs out of a factor cache.

ParameterType
valuenumber
fromstring
tostring

number