Documentation
    Preparing search index...

    Namespace MapUtil

    A namespace providing utility functions for Map manipulation.

    This namespace contains helper functions for working with JavaScript Map objects, providing convenient methods for common Map operations like retrieving values with lazy initialization.

    Processing cost: The get-or-create operation uses native Map membership and lookup, and calls the factory only for an absent key; its dominant extra cost is the caller factory.

    Reuse: Stored values, including promises and undefined, are reused while the supplied Map retains the key. The caller must key every relevant dependency and remove stale or rejected entries; this helper cannot establish external-data freshness or prevent reentrant factory calls.

    Resource ownership: New values are transferred into the caller-owned Map, whose growth and eviction remain the caller responsibility; the helper acquires no external handle or background task.

    Jeongho Nam - https://github.com/samchon

    // Create a cache with lazy initialization
    const cache = new Map<string, ExpensiveObject>();

    const obj = MapUtil.take(cache, "key1", () => {
    console.log("Creating expensive object...");
    return new ExpensiveObject();
    });

    // Subsequent calls return cached value without re-creating
    const sameObj = MapUtil.take(cache, "key1", () => new ExpensiveObject());
    console.log(obj === sameObj); // true

    contracts/common.md#principled-implementation The namespace offers the get-or-create pattern over the native Map, whose has distinguishes an absent key from a stored undefined.

    contracts/common.md#clear-and-simple-design One function, without options or state.

    contracts/common.md#prohibited-implementation-shortcuts It uses only the public Map methods.

    contracts/common.md#meaningful-documentation The comment states the lazy initialization pattern with an example.

    take