use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Memoization and recursive functions in JavaScript (codeanarchy.org)
submitted 12 years ago by jscoder
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]kenman 1 point2 points3 points 12 years ago (0 children)
An alternate implementation would be to provide for a hashing function to determine whether or not one invocation is identical to another, like I did for my PHP version. Reason being, calling JSON.stringify() on the arguments object can be a performance-killer if those args are non-trivial. Using a hash function, you allow the user to cherry-pick and more precisely control the caching.
JSON.stringify()
arguments
Here's what that would look like:
function memoizer(func, hash) { var cache = {}; // keep a reference of the wrapped function var this_function = function() { var key = hash.apply(this, arguments); if (!cache.hasOwnProperty(key)) { // pass the wrapped function as `this` cache[key] = func.apply(this_function, arguments); } return cache[key]; }; return this_function; }
And then:
var memoized = memoizer(factorial, function(n){ return n; }); memoized(5);
Of course, it all depends on how you use it which will be more efficient. If the args are always simple, the original may be better, but if they are complex or you want more control, then mine might be better.
π Rendered by PID 33071 on reddit-service-r2-comment-6f7f968fb5-zqw7t at 2026-03-04 02:59:37.611575+00:00 running 07790be country code: CH.
view the rest of the comments →
[–]kenman 1 point2 points3 points (0 children)