memoizerific
Advanced tools
| { | ||
| "spec_dir": "tests", | ||
| "spec_files": [ | ||
| "*" | ||
| ], | ||
| "stopSpecOnExpectationFailure": false, | ||
| "random": false | ||
| } |
| var Memoizerific = require('../src/memoizerific'); | ||
| describe("fibonacci", () => { | ||
| var fibonacci, | ||
| fibonacciMemoized, | ||
| fibonacciResult, | ||
| fibonacciMemoizedResult, | ||
| fibonacciTime, | ||
| fibonacciMemoizedTime; | ||
| fibonacci = function (n) { | ||
| if (n < 2){ | ||
| return 1; | ||
| } | ||
| else { | ||
| return fibonacci(n-2) + fibonacci(n-1); | ||
| } | ||
| }; | ||
| fibonacciMemoized = Memoizerific(50)(function (n) { | ||
| if (n < 2){ | ||
| return 1; | ||
| } | ||
| else { | ||
| return fibonacciMemoized(n-2) + fibonacciMemoized(n-1); | ||
| } | ||
| }); | ||
| fibonacciTime = process.hrtime(); | ||
| fibonacciResult = fibonacci(40); | ||
| fibonacciTime = process.hrtime(fibonacciTime); | ||
| fibonacciMemoizedTime = process.hrtime(); | ||
| fibonacciMemoizedResult = fibonacciMemoized(40); | ||
| fibonacciMemoizedTime = process.hrtime(fibonacciMemoizedTime); | ||
| it("should be map or similar", () => { expect(fibonacciMemoized.cache instanceof Map).toEqual(process.env.TEST_MAPORSIMILAR !== 'true'); }); | ||
| it("should equal non-memoized result", () => { expect(fibonacciResult).toEqual(fibonacciMemoizedResult); }); | ||
| it("should have proper lru length", () => { expect(fibonacciMemoized.lru.length).toEqual(41); }); | ||
| it("should have significantly higher performance", () => { expect(fibonacciTime[0] - fibonacciMemoizedTime[0] >= 2).toEqual(true); }); | ||
| }); | ||
| describe("complex args", () => { | ||
| var memoizedFn, | ||
| arg1 = { a: { b: 3 }, num: 3 }, | ||
| arg2 = { c: { d: 3 }, num: 7 }, | ||
| arg3 = [{ f: { g: 3 }, num: 11 }, { h: { i: 3 }, num: 4 }, { j: { k: 3 }, num: 6 }]; | ||
| beforeEach(function() { | ||
| memoizedFn = Memoizerific(50)(function(arg1, arg2, arg3) { | ||
| return arg1.num * arg2.num; | ||
| }); | ||
| memoizedFn(arg1, arg2, arg3); | ||
| }); | ||
| it("should be map or similar", () => { expect(memoizedFn.cache instanceof Map).toEqual(process.env.TEST_MAPORSIMILAR !== 'true'); }); | ||
| it("should not be memoized", () => { | ||
| expect(memoizedFn.wasMemoized).toEqual(false); | ||
| expect(memoizedFn.lru.length).toEqual(1); | ||
| }); | ||
| it("should be memoized", () => { | ||
| memoizedFn(arg1, arg2, arg3); | ||
| expect(memoizedFn.wasMemoized).toEqual(true); | ||
| expect(memoizedFn.lru.length).toEqual(1); | ||
| }); | ||
| it("should have multiple cached items", () => { | ||
| memoizedFn(arg1, arg2, arg3); | ||
| memoizedFn(arg1, arg2, 1); | ||
| expect(memoizedFn.wasMemoized).toEqual(false); | ||
| expect(memoizedFn.lru.length).toEqual(2); | ||
| }); | ||
| }); |
+81
-34
| (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){ | ||
| if (typeof Map === 'function') { | ||
| module.exports = Map; | ||
| if (typeof Map !== 'function' || (process && process.env && process.env.TEST_MAPORSIMILAR === 'true')) { | ||
| module.exports = _dereq_('./similar'); | ||
| } | ||
| else { | ||
| module.exports = Similar; | ||
| module.exports = Map; | ||
| } | ||
| },{"./similar":2}],2:[function(_dereq_,module,exports){ | ||
| function Similar() { | ||
| this.list = []; | ||
| this.lastHas = null; | ||
| this.lastItem = undefined; | ||
| this.size = 0; | ||
| return this; | ||
@@ -18,22 +18,36 @@ } | ||
| Similar.prototype.get = function(key) { | ||
| var len = this.list.length, | ||
| i; | ||
| if (this.lastGet && this.lastGet.key === key) { | ||
| return this.lastGet.val; | ||
| var index; | ||
| if (this.lastItem && this.lastItem.key === key) { | ||
| return this.lastItem.val; | ||
| } | ||
| for (i = 0; i < len; i++) { | ||
| if (this.list[i].key === key) { | ||
| this.lastGet = this.list[i]; | ||
| return this.list[i]; | ||
| } | ||
| index = this.indexOf(key); | ||
| if (index >= 0) { | ||
| this.lastItem = this.list[index]; | ||
| return this.list[index].val; | ||
| } | ||
| return null; | ||
| return undefined; | ||
| }; | ||
| Similar.prototype.set = function(key, val) { | ||
| this.list.push({ key: key, val: val }); | ||
| var index; | ||
| if (this.lastItem && this.lastItem.key === key) { | ||
| this.lastItem.val = val; | ||
| return this; | ||
| } | ||
| index = this.indexOf(key); | ||
| if (index >= 0) { | ||
| this.lastItem = this.list[index]; | ||
| this.list[index].val = val; | ||
| return this; | ||
| } | ||
| this.lastItem = { key: key, val: val }; | ||
| this.list.push(this.lastItem); | ||
| this.size++; | ||
| return this; | ||
@@ -43,21 +57,49 @@ }; | ||
| Similar.prototype.delete = function(key) { | ||
| var len = this.list.length, | ||
| i; | ||
| for (i = 0; i < len; i++) { | ||
| if (this.list[i].key === key) { | ||
| break; | ||
| } | ||
| var index; | ||
| if (this.lastItem && this.lastItem.key === key) { | ||
| this.lastItem = undefined; | ||
| } | ||
| if (this.list.splice(i, 1).length) { | ||
| index = this.indexOf(key); | ||
| if (index >= 0) { | ||
| this.size--; | ||
| return this.list.splice(index, 1)[0]; | ||
| } | ||
| return this; | ||
| return undefined; | ||
| }; | ||
| Similar.prototype.has = Similar.prototype.get; | ||
| },{}],2:[function(_dereq_,module,exports){ | ||
| var MapOrSimilar = _dereq_('./maporsimilar'); | ||
| // important that has() doesn't use get() in case an existing key has a falsy value, in which case has() would return false | ||
| Similar.prototype.has = function(key) { | ||
| var index; | ||
| if (this.lastItem && this.lastItem.key === key) { | ||
| return true; | ||
| } | ||
| index = this.indexOf(key); | ||
| if (index >= 0) { | ||
| this.lastItem = this.list[index]; | ||
| return true; | ||
| } | ||
| return false; | ||
| }; | ||
| Similar.prototype.indexOf = function(key) { | ||
| var i; | ||
| for (i = 0; i < this.size; i++) { | ||
| if (this.list[i].key === key) { | ||
| return i; | ||
| } | ||
| } | ||
| return -1; | ||
| }; | ||
| module.exports = Similar; | ||
| },{}],3:[function(_dereq_,module,exports){ | ||
| var MapOrSimilar = _dereq_('map-or-similar'); | ||
| module.exports = function (limit) { | ||
@@ -125,5 +167,10 @@ var cache = new MapOrSimilar(), | ||
| memoizerific.wasMemoized = isMemoized; | ||
| return fnResult; | ||
| }; | ||
| memoizerific.cache = cache; | ||
| memoizerific.lru = lru; | ||
| return memoizerific; | ||
@@ -178,3 +225,3 @@ }; | ||
| } | ||
| },{"./maporsimilar":1}]},{},[2])(2) | ||
| },{"map-or-similar":1}]},{},[3])(3) | ||
| }); |
@@ -1,7 +0,7 @@ | ||
| � �Uێ�6�[ | ||
| Ӫ��Ri�h��d_�'A�2�bC�*Ee����;��kc�� ��9s�hTn�V��V(��������b�����u�:�B(~ | ||
| ��dz>5��� ��"�pY���g�%�g��'�Q��_m�1�� ���> u�O����x����c���QsY�OlKQ��Y��܈B䞁sh����ƨ�hYYĉ! | ||
| ��E MX'�ڤ:�O�;y. �����6�!�wX6x�� �z���m�`�U�i��mP�+SJ�= �Y�6z�pbK��VE��������>|�t�����?z��y<I}�*��%�C�ˢ�I��Xd�����q��6K�T2G[�$�MKz����C�]L<\�#zMw��YE��G[&z��)/���C�>N�l}�E����IQ[���{a����T5R��Z����GV�9��x~SDE��V{��[�ZBȌNI����S.�s�˷�+�_P�}c2�rB9�0yuF4�����t�H�/��^w���`E���Q��%j!nl ds�gi7�^�%h���ُ ��<�5�]�N��*���"�0�{<@ �}b��ubQ�[㲦��.#wo6\�K72DA`_�����A% | ||
| �m�������������R�E�<9>Ax��'�����[la��[�Y\����H�}�e&`�%����\���P����.��f2���jt^�5�4��l��-h�o���3����YHf`��sv�$ݎ�!�t���G�>�.v}g )����"7g�l=H�ݓ��b{F�f�IU�T)�2�H | ||
| �L��G'*�$ OP���� ��ᣤ����#��'l`"��]x�?$˦��A���%�7W�`�=8�xyA ��*����7�eF�{�E���-�%�͏ | ||
| R���-XA��`��_ET���^�m�Ȅ93�����e~�Hz�at�N���+� | ||
| �p��Vmemoizerific.min.js �V[o�6~߯����0��ɛT���4C�>B��Tĕ�4�J����;�͖���bQG������T��BoE����O[�R�Z�<��E���L�Rmx"�x��c�oJ�q��[Uj�4|��:�=b�2�4O�e��:"���}fz�CN�Y}j����GpN�I�L���Y �e�r?�M�!��x��\�D��B}hpM�ND��-�u�F����%1����5���O��gb��_�����;�dR��^ ��9vr��T+s� U�et�u���S*�#pyӦh�Λ����áMu�2J�8�@xo�>|�x����������>x$����;ݶ9 �U:��ȏ��Hv�&�� | ||
| Z+.����pi$V"��"��o���U��3�}�����/��<d�/�z�iȦS��qA�)Th��{��8����ڸe�nW�<��=�\=CX]r�A�������۫?�>����x�yu������L{8� #�|.��\����@������0����_��7�g�9�Ѽ(hڜ+�e�;�'n�q�x8�n�&��������v5�>C�!Nk!��~� ,�K:_�!����w̛���=��!]3�Kx[f��0:aLymH�0o2��E4^ڂ�� ^�o�Eiҡ*ާl:m��°�[�s��Lйr�\��=Gj6;�o | ||
| )b�o�a ti�M���"��WU�HƋ�� g�9]����!� ä�g˪��+�EE�]m����/�l�} S�,b((�jZ | ||
| h�� | ||
| ��:���y=�̒�3Α\���M��ƴ��������[Z7Ǯ���6�����B�gD�|���!�f&` �)����%��Y�)�]�����i�E�a�t�c�ۍ��Ü���?�V�*�q��b��Y?S�}�O�[/E�����F����њ/��XS)�eƕ���lA2z�5{E�t�I�Q��( '��I�.`q�~%����)]{�C^���c3��,H�zR�a8� &%M�5C؏�S@�'� 88��!I�ϊB�"UJIz5|b3���0��.����l | ||
| ��+��p ��Y'ڐ.a"�j2�H�O���3�͟� ���:��6 =�/uIɫ��У�U�ڐ�/#�.q��_��Fq� | ||