Skip to content

@graphty/graphty-element / logging / lazy

Function: lazy() ​

lazy<T>(fn): LazyValue<T>

Defined in: graphty-element/src/logging/LazyEval.ts:73

Wrap an expensive computation for lazy evaluation.

The provided function will only be called when the lazy value is invoked. After the first call, the result is cached and returned on subsequent calls.

Type Parameters ​

T ​

T

Parameters ​

fn ​

() => T

Function that computes the value

Returns ​

LazyValue<T>

A lazy wrapper function that computes and caches the value on first call

Example ​

typescript
// Basic usage
const lazyData = lazy(() => computeExpensiveData());
logger.debug("Expensive operation", { data: lazyData });

// The lazy function can be called manually to get the value
const value = lazyData(); // Computes on first call
const cachedValue = lazyData(); // Returns cached value

Remarks ​

Performance Guidelines:

  1. Use for expensive computations: Only wrap computations that are expensive (serialization, large object traversal, etc.)

  2. Combine with level guards: For maximum efficiency, use isDebugEnabled() or isTraceEnabled() guards to avoid even creating the lazy wrapper:

    typescript
    if (logger.isDebugEnabled()) {
        logger.debug("Data", { complex: lazy(() => serialize(node)) });
    }
  3. Avoid in hot paths: Even the lazy wrapper creation has some overhead. In render loops or per-frame callbacks, use level guards without lazy.