@metreeca/core - v0.13.0
    Preparing search index...

    Function memo

    • Defers keyed values to first use.

      Wraps a factory in a keyed accessor that computes the value for a key on the first request and reuses the result thereafter, so work the caller may never need is neither done upfront nor repeated. All callers requesting the same key share the same value, and a value of undefined is kept and reused like any other.

      Keys are compared as Map compares them, with SameValueZero equality: NaN matches NaN and 0 matches -0. Object keys match by identity, so equal but distinct objects get separate values from the factory.

      Warning

      Values are kept for the lifetime of the accessor and never evicted. Memory use grows with every distinct key requested, so the accessor is suited to key sets known to stay small or bounded over the accessor's lifetime.

      Important

      A factory call that throws is not kept, and the next request for that key calls the factory again. A rejected promise is a value like any other: it is kept, and every later request for its key gets the same rejection.

      Type Parameters

      • K

        The type of the keys

      • V

        The type of the deferred values

      Parameters

      • factory: (key: K) => V

        The factory computing the value for a key, called at most once per key unless it throws

      Returns (key: K) => V

      An accessor returning the value for a key, computing it on the first request for that key

      Whatever factory throws while computing a requested value