/**
 * Description: This is a SoundFont2 parser.
 *
 * @package     midi-Synth
 * @category    module js
 * @author      Mrtenz <https://github.com/Mrtenz/soundfont2>
 * @copyright   soundfont2
 * @license     GPL-2.0-or-later
 */

/**
 * Returns a memoized function for the original function. Function arguments are serialized as a
 * JSON string and stored in an in-memory object.
 *
 * @template T
 * @template U
 * @param {(...originalArgs: T[]) => U} originalFunction
 */
const memoize = originalFunction => {
  const memo = {}

  return (...args) => {
    const serializedArgs = JSON.stringify(args)
    if (serializedArgs in memo) {
      return memo[serializedArgs]
    }

    const output = originalFunction(...args)
    memo[serializedArgs] = output
    return output
  }
}
