All files / utils memoization.js

100% Statements 18/18
100% Branches 4/4
100% Functions 3/3
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 192x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 17x 17x 17x 17x 17x 17x 17x  
/**
 * Memoization technique that calls the callback once, subsequent calls
 * simply returns the result of the first callback
 *
 * No arguments are passed to the callback
 *
 * @template {() => unknown} T
 * @param {T} callback - callback to memoize
 * @returns {() => ReturnType<T>} memoized function
 */
export function computeOnce (callback) {
  let call = () => {
    const result = /** @type {ReturnType<T>} */(callback())
    call = () => result
    return result
  }
  return () => call()
}