{"ScriptPreparationCode":null,"TestCases":[{"Name":"Fib Base","Code":"const fibonacci = (n) =\u003E {\r\n\tif (n \u003C= 1) return 1;\r\n\treturn fibonacci(n - 1) \u002B fibonacci(n - 2);\r\n};\r\n\r\nfibonacci(20);\r\n","IsDeferred":false},{"Name":"Fib memoized 1","Code":"const memoize = function(func) {\r\n\tvar stringifyJson = JSON.stringify,\r\n\t\tcache = {};\r\n\r\n\tvar cachedfun = function() {\r\n\t\tvar hash = stringifyJson(arguments);\r\n\t\treturn hash in cache ? cache[hash] : (cache[hash] = func.apply(this, arguments));\r\n\t};\r\n\r\n\tcachedfun.__cache = function() {\r\n\t\tcache.remove ||\r\n\t\t\t(cache.remove = function() {\r\n\t\t\t\tvar hash = stringifyJson(arguments);\r\n\t\t\t\treturn delete cache[hash];\r\n\t\t\t});\r\n\t\treturn cache;\r\n\t}.call(this);\r\n\r\n\treturn cachedfun;\r\n};\r\n\r\nconst fibonacci = memoize((n) =\u003E {\r\n\tif (n \u003C= 1) return 1;\r\n\treturn fibonacci(n - 1) \u002B fibonacci(n - 2);\r\n});\r\n\r\nfibonacci(20);\r\n","IsDeferred":false},{"Name":"Fib memoized Tommy","Code":"const memoize = function(func) {\r\n\tconst cache= {}\r\n return (...args) =\u003E {\r\n const n = args[0]\r\n if (n in cache) {\r\n return cache[n];\r\n } else {\r\n const result = func(n)\r\n cache[n] = result\r\n return result\r\n }\r\n }\r\n};\r\n\r\nconst fibonacci = memoize((n) =\u003E {\r\n\tif (n \u003C= 1) return 1;\r\n\treturn fibonacci(n - 1) \u002B fibonacci(n - 2);\r\n});\r\n\r\nfibonacci(20);\r\n","IsDeferred":false},{"Name":"Fib fast-memoize","Code":"//\r\n// Main\r\n//\r\n\r\nfunction memoize (fn, options) {\r\n var cache = options \u0026\u0026 options.cache\r\n ? options.cache\r\n : cacheDefault\r\n\r\n var serializer = options \u0026\u0026 options.serializer\r\n ? options.serializer\r\n : serializerDefault\r\n\r\n var strategy = options \u0026\u0026 options.strategy\r\n ? options.strategy\r\n : strategyDefault\r\n\r\n return strategy(fn, {\r\n cache: cache,\r\n serializer: serializer\r\n })\r\n}\r\n\r\n//\r\n// Strategy\r\n//\r\n\r\nfunction isPrimitive (value) {\r\n return value == null || typeof value === \u0027number\u0027 || typeof value === \u0027boolean\u0027 // || typeof value === \u0022string\u0022 \u0027unsafe\u0027 primitive for our needs\r\n}\r\n\r\nfunction monadic (fn, cache, serializer, arg) {\r\n var cacheKey = isPrimitive(arg) ? arg : serializer(arg)\r\n\r\n var computedValue = cache.get(cacheKey)\r\n if (typeof computedValue === \u0027undefined\u0027) {\r\n computedValue = fn.call(this, arg)\r\n cache.set(cacheKey, computedValue)\r\n }\r\n\r\n return computedValue\r\n}\r\n\r\nfunction variadic (fn, cache, serializer) {\r\n var args = Array.prototype.slice.call(arguments, 3)\r\n var cacheKey = serializer(args)\r\n\r\n var computedValue = cache.get(cacheKey)\r\n if (typeof computedValue === \u0027undefined\u0027) {\r\n computedValue = fn.apply(this, args)\r\n cache.set(cacheKey, computedValue)\r\n }\r\n\r\n return computedValue\r\n}\r\n\r\nfunction assemble (fn, context, strategy, cache, serialize) {\r\n return strategy.bind(\r\n context,\r\n fn,\r\n cache,\r\n serialize\r\n )\r\n}\r\n\r\nfunction strategyDefault (fn, options) {\r\n var strategy = fn.length === 1 ? monadic : variadic\r\n\r\n return assemble(\r\n fn,\r\n this,\r\n strategy,\r\n options.cache.create(),\r\n options.serializer\r\n )\r\n}\r\n\r\nfunction strategyVariadic (fn, options) {\r\n var strategy = variadic\r\n\r\n return assemble(\r\n fn,\r\n this,\r\n strategy,\r\n options.cache.create(),\r\n options.serializer\r\n )\r\n}\r\n\r\nfunction strategyMonadic (fn, options) {\r\n var strategy = monadic\r\n\r\n return assemble(\r\n fn,\r\n this,\r\n strategy,\r\n options.cache.create(),\r\n options.serializer\r\n )\r\n}\r\n\r\n//\r\n// Serializer\r\n//\r\n\r\nfunction serializerDefault () {\r\n return JSON.stringify(arguments)\r\n}\r\n\r\n//\r\n// Cache\r\n//\r\n\r\nfunction ObjectWithoutPrototypeCache () {\r\n this.cache = Object.create(null)\r\n}\r\n\r\nObjectWithoutPrototypeCache.prototype.has = function (key) {\r\n return (key in this.cache)\r\n}\r\n\r\nObjectWithoutPrototypeCache.prototype.get = function (key) {\r\n return this.cache[key]\r\n}\r\n\r\nObjectWithoutPrototypeCache.prototype.set = function (key, value) {\r\n this.cache[key] = value\r\n}\r\n\r\nvar cacheDefault = {\r\n create: function create () {\r\n return new ObjectWithoutPrototypeCache()\r\n }\r\n}\r\n\r\n\r\nconst fibonacci = memoize((n) =\u003E {\r\n\tif (n \u003C= 1) return 1;\r\n\treturn fibonacci(n - 1) \u002B fibonacci(n - 2);\r\n});\r\n\r\nfibonacci(20);\r\n","IsDeferred":false}]}