memoizerific.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.memoizerific = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
  2. module.exports = function(forceSimilar) {
  3. if (typeof Map !== 'function' || forceSimilar) {
  4. var Similar = _dereq_('./similar');
  5. return new Similar();
  6. }
  7. else {
  8. return new Map();
  9. }
  10. }
  11. },{"./similar":2}],2:[function(_dereq_,module,exports){
  12. function Similar() {
  13. this.list = [];
  14. this.lastItem = undefined;
  15. this.size = 0;
  16. return this;
  17. }
  18. Similar.prototype.get = function(key) {
  19. var index;
  20. if (this.lastItem && this.isEqual(this.lastItem.key, key)) {
  21. return this.lastItem.val;
  22. }
  23. index = this.indexOf(key);
  24. if (index >= 0) {
  25. this.lastItem = this.list[index];
  26. return this.list[index].val;
  27. }
  28. return undefined;
  29. };
  30. Similar.prototype.set = function(key, val) {
  31. var index;
  32. if (this.lastItem && this.isEqual(this.lastItem.key, key)) {
  33. this.lastItem.val = val;
  34. return this;
  35. }
  36. index = this.indexOf(key);
  37. if (index >= 0) {
  38. this.lastItem = this.list[index];
  39. this.list[index].val = val;
  40. return this;
  41. }
  42. this.lastItem = { key: key, val: val };
  43. this.list.push(this.lastItem);
  44. this.size++;
  45. return this;
  46. };
  47. Similar.prototype.delete = function(key) {
  48. var index;
  49. if (this.lastItem && this.isEqual(this.lastItem.key, key)) {
  50. this.lastItem = undefined;
  51. }
  52. index = this.indexOf(key);
  53. if (index >= 0) {
  54. this.size--;
  55. return this.list.splice(index, 1)[0];
  56. }
  57. return undefined;
  58. };
  59. // important that has() doesn't use get() in case an existing key has a falsy value, in which case has() would return false
  60. Similar.prototype.has = function(key) {
  61. var index;
  62. if (this.lastItem && this.isEqual(this.lastItem.key, key)) {
  63. return true;
  64. }
  65. index = this.indexOf(key);
  66. if (index >= 0) {
  67. this.lastItem = this.list[index];
  68. return true;
  69. }
  70. return false;
  71. };
  72. Similar.prototype.forEach = function(callback, thisArg) {
  73. var i;
  74. for (i = 0; i < this.size; i++) {
  75. callback.call(thisArg || this, this.list[i].val, this.list[i].key, this);
  76. }
  77. };
  78. Similar.prototype.indexOf = function(key) {
  79. var i;
  80. for (i = 0; i < this.size; i++) {
  81. if (this.isEqual(this.list[i].key, key)) {
  82. return i;
  83. }
  84. }
  85. return -1;
  86. };
  87. // check if the numbers are equal, or whether they are both precisely NaN (isNaN returns true for all non-numbers)
  88. Similar.prototype.isEqual = function(val1, val2) {
  89. return val1 === val2 || (val1 !== val1 && val2 !== val2);
  90. };
  91. module.exports = Similar;
  92. },{}],3:[function(_dereq_,module,exports){
  93. var MapOrSimilar = _dereq_('map-or-similar');
  94. module.exports = function (limit) {
  95. var cache = new MapOrSimilar(undefined === 'true'),
  96. lru = [];
  97. return function (fn) {
  98. var memoizerific = function () {
  99. var currentCache = cache,
  100. newMap,
  101. fnResult,
  102. argsLengthMinusOne = arguments.length - 1,
  103. lruPath = Array(argsLengthMinusOne + 1),
  104. isMemoized = true,
  105. i;
  106. if ((memoizerific.numArgs || memoizerific.numArgs === 0) && memoizerific.numArgs !== argsLengthMinusOne + 1) {
  107. throw new Error('Memoizerific functions should always be called with the same number of arguments');
  108. }
  109. // loop through each argument to traverse the map tree
  110. for (i = 0; i < argsLengthMinusOne; i++) {
  111. lruPath[i] = {
  112. cacheItem: currentCache,
  113. arg: arguments[i]
  114. };
  115. // climb through the hierarchical map tree until the second-last argument has been found, or an argument is missing.
  116. // if all arguments up to the second-last have been found, this will potentially be a cache hit (determined later)
  117. if (currentCache.has(arguments[i])) {
  118. currentCache = currentCache.get(arguments[i]);
  119. continue;
  120. }
  121. isMemoized = false;
  122. // make maps until last value
  123. newMap = new MapOrSimilar(undefined === 'true');
  124. currentCache.set(arguments[i], newMap);
  125. currentCache = newMap;
  126. }
  127. // we are at the last arg, check if it is really memoized
  128. if (isMemoized) {
  129. if (currentCache.has(arguments[argsLengthMinusOne])) {
  130. fnResult = currentCache.get(arguments[argsLengthMinusOne]);
  131. }
  132. else {
  133. isMemoized = false;
  134. }
  135. }
  136. if (!isMemoized) {
  137. fnResult = fn.apply(null, arguments);
  138. currentCache.set(arguments[argsLengthMinusOne], fnResult);
  139. }
  140. if (limit > 0) {
  141. lruPath[argsLengthMinusOne] = {
  142. cacheItem: currentCache,
  143. arg: arguments[argsLengthMinusOne]
  144. };
  145. if (isMemoized) {
  146. moveToMostRecentLru(lru, lruPath);
  147. }
  148. else {
  149. lru.push(lruPath);
  150. }
  151. if (lru.length > limit) {
  152. removeCachedResult(lru.shift());
  153. }
  154. }
  155. memoizerific.wasMemoized = isMemoized;
  156. memoizerific.numArgs = argsLengthMinusOne + 1;
  157. return fnResult;
  158. };
  159. memoizerific.limit = limit;
  160. memoizerific.wasMemoized = false;
  161. memoizerific.cache = cache;
  162. memoizerific.lru = lru;
  163. return memoizerific;
  164. };
  165. };
  166. // move current args to most recent position
  167. function moveToMostRecentLru(lru, lruPath) {
  168. var lruLen = lru.length,
  169. lruPathLen = lruPath.length,
  170. isMatch,
  171. i, ii;
  172. for (i = 0; i < lruLen; i++) {
  173. isMatch = true;
  174. for (ii = 0; ii < lruPathLen; ii++) {
  175. if (!isEqual(lru[i][ii].arg, lruPath[ii].arg)) {
  176. isMatch = false;
  177. break;
  178. }
  179. }
  180. if (isMatch) {
  181. break;
  182. }
  183. }
  184. lru.push(lru.splice(i, 1)[0]);
  185. }
  186. // remove least recently used cache item and all dead branches
  187. function removeCachedResult(removedLru) {
  188. var removedLruLen = removedLru.length,
  189. currentLru = removedLru[removedLruLen - 1],
  190. tmp,
  191. i;
  192. currentLru.cacheItem.delete(currentLru.arg);
  193. // walk down the tree removing dead branches (size 0) along the way
  194. for (i = removedLruLen - 2; i >= 0; i--) {
  195. currentLru = removedLru[i];
  196. tmp = currentLru.cacheItem.get(currentLru.arg);
  197. if (!tmp || !tmp.size) {
  198. currentLru.cacheItem.delete(currentLru.arg);
  199. } else {
  200. break;
  201. }
  202. }
  203. }
  204. // check if the numbers are equal, or whether they are both precisely NaN (isNaN returns true for all non-numbers)
  205. function isEqual(val1, val2) {
  206. return val1 === val2 || (val1 !== val1 && val2 !== val2);
  207. }
  208. },{"map-or-similar":1}]},{},[3])(3)
  209. });