Issue #188 (func.props, CSS Tools, Testing/Debugging, Uncats)

In the book Secrets of the JavaScript Ninja, the authors discuss a technique called memoization, in which they make use of a JavaScript feature I wasn't previously aware of.

If you want a good intro to memoization, this article by Addy Osmani is a good start or you can grab a copy of the aforementioned book. The specific feature I'm referring to, however, is used in the book and in Addy's article.

Briefly, memoization is a way to allow a function to remember previously computed results, so as to avoid repeating highly-intensive computations unnecessarily. So a memoization function will cache results and this can be done by taking advantage of the fact that functions are objects on which you can attach properties.

Below is a rudimentary example of the basic principle of using function properties to cache values so you'll know if the function has already been run. Note that this is not a proper memoization technique, this is just to demonstrate the properties-on-functions technique:

In this code, the doSomething() function (paradoxically) does nothing. But each time I call it, I'm attaching the incrementing value of the id variable to that function's "done" property. This gives the function a sort of memory of itself, so I can tell how many times it's been called. In the demo you can see that each log has a new value.

As mentioned, this is not proper memoization, this is just a silly example to demonstrate that you can attach properties to functions, and how this might be useful. The book discusses this in greater detail and there are lots of sources online, including the aforementioned article, for a better understanding of memoization. You might also want to check out this memoization tool.