Optimize the functions in javascript

If we have the function:


and we launch test(1,2) we will get 3 as a result.
If this function is for example inside a loop which could execute it again with the exact same parameters (i.e. test(1,2)) always obtaining the same result (3), we will have executed a function uselessly because it has already been launched previously, therefore, using computing power for nothing.

While doing:


we will always have the result 3 but the function will be executed only once and the value stored, at subsequent uses the value in memory will be shown without re-executing the function.

In case we had the function:


we will use the object like this:


Here is the code of optimizeFunction