javascript memoization

I am using the word, memoization, because it is cool and it sounds like a buzzword, and author of the javascript ninja also called it so.

 

 

so, what is memoization. " Memoization is the process of building a function which is capable of remembering its previously  computed answers. "

 

 

so let 's see some sample implementation that realize the memoization. 

 

 

/**************************************
*@Summary
*  this is the js file that demonstrate the use of the memoization (memorization), I called it memoization as it is like some buzzword as Ninja is calling it so.
*
* Memorization is a very common technique that you can save some compute values as a cache, and you can shortcut some long operation if a value has already computed
*
* @Todo:
*   this file is not created, will come back to it with implementation

***************************************/


/**
* @Summary
*   memoized
* @Usage: 
*  isPrime.memoized(5)...
*  isPrime._values[5]
*/
Function.prototype.memoized = function (key) {
  this._values = this._values || {};  // this is a common technique to initialize something that if it does not have a default value, assign one 
  return this._values[key] !== undefined ?
      this._values[key] :
      this._values[key] = this.apply(this, arguments); // the reason why not to store this a fn and so some manipulation is because 'this' we are just using the same function
};

/**
* @Summary
*   memoize
* @Usage: 
*  var isPrime = (function(num) { 
     .. 
}).memoize();
* isPrime(5)
*/
Function.prototype.memoize = function () {
  var fn = this;
  return function () {
    return fn.memoized.apply(fn, arguments);
  };
};




/**
* @Summary
*   isPrime
* @Comment: this is the function that we test for memoization
*/
function isPrime(num) {
  var prime = num != 1;
  for (var i = 2; i < num; i++) {
    if (num % i == 0) {
      prime = false;
      break;
    }
  }
  return prime;
}

 

 

and the following code that test the memoizaion. 

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type ="text/javascript" src="memoization.js"></script>
    <script type="text/javascript" src="../unit.js"></script>
    <script type="text/javascript">

      window.onload = function () {
        test("memoization test", function () {

          var isPrime_moized = isPrime.memoize();

          assert(isPrime_moized(5), "Make sure the function works, 5 is prime.");
          assert(isPrime._values[5], "Make sure the answer is cached.");

          assert(isPrime.memoized(5), "Make sure the function works, 5 is prime.");
          assert(isPrime._values[5], "Make sure the answer is cached.");
        });
      };
    </script>

    <style type="text/css" >
      #results li.pass { color: Green }
      #results li.fail { color: Red }
    </style>

</head>
<body>
<ul id="results" />
</body>
</html>
 

It is believed that with the memoization, you can achieve faster process (trade off spaces for time.)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值