@oazmi/kitchensink - v0.9.13
    Preparing search index...

    Function memorize

    • memorize the return value of a single parameter function. further calls with memorized arguments will return the value much quicker.

      Type Parameters

      • V
      • K

      Parameters

      • fn: (arg: K) => V

      Returns (arg: K) => V

      import { assertEquals as assertEq } from "jsr:@std/assert"

      let fn_call_count = 0
      const fn = (arg: string | number) => {
      fn_call_count++
      return `you owe ${arg} to the bank.`
      }
      const memorized_fn = memorize(fn)

      assertEq(memorized_fn("a camel") , "you owe a camel to the bank.")
      assertEq(fn_call_count , 1)
      assertEq(memorized_fn("a camel") , "you owe a camel to the bank.")
      assertEq(fn_call_count , 1)
      assertEq(memorized_fn("5") , "you owe 5 to the bank.")
      assertEq(fn_call_count , 2)
      assertEq(memorized_fn(5) , "you owe 5 to the bank.")
      assertEq(fn_call_count , 3)
      assertEq(memorized_fn("your soul"), "you owe your soul to the bank.")
      assertEq(fn_call_count , 4)
      assertEq(memorized_fn("your soul"), "you owe your soul to the bank.")
      assertEq(fn_call_count , 4)