portal entry

select a category, or use search below
(searches all categories and all time range)
Title:

memoize functions in CFML

| View in Portal
August 29, 2019 03:47:31 PM GMT
6 Comments
<p>Memoize functions in CFML</p>
<p>The post <a rel="nofollow" href="https://coldfusion.adobe.com/2019/08/memoize-functions-cfml/">memoize functions in CFML</a> appeared first on <a rel="nofollow" href="https://coldfusion.adobe.com">ColdFusion</a>.</p>
Labels: Blog, ColdFusion, Language, 2018, blog, language, modern cfml

Comments:

<p>That’s very interesting, John. I’d not heard of memoization before. I see it’s been around a long time (<a href="https://en.wikipedia.org/wiki/Memoization" rel="nofollow">https://en.wikipedia.org/wiki/Memoization</a>). And I see that it relies upon the concept of closures (<a href="https://helpx.adobe.com/coldfusion/developing-applications/the-cfml-programming-language/extending-coldfusion-pages-with-cfml-scripting/using-closures.html" rel="nofollow">https://helpx.adobe.com/coldfusion/developing-applications/the-cfml-programming-language/extending-coldfusion-pages-with-cfml-scripting/using-closures.html</a>), which were introduced in CF10. Just adding these additional resources in case they may help others coming upon this and wishing to learn more.</p><p>Thanks for sharing it and the working examples. (I have some more thoughts I will share separately.)</p>
Comment by Charlie Arehart
2275 | September 04, 2019 02:25:02 PM GMT
<p>Following on my previous comment, I wanted to share some more thoughts, again in case they may help others seeing this post and considering using it.</p><p>First, it should be noted as well that this form of lightweight caching lives only for the life of the request (at least as written here). Folks who run his cffiddle example will see this more clearly, as each call to the page shows the first and third method calls always taking about a second, even on page refreshes. It’s the subsequent calls with the same args IN THE SAME REQUEST which show the speed boost.</p><p>And it can indeed have value to do such saving of calls within a single request. I just want to help readers realize that it will not help for repeated requests OF that given template.</p><p>Someone may be tempted to assert that this could be addressed by storing into a shared scope (session, application, or server) the variable holding the saved results (literally “results” in his code above). And sure, one COULD, but then you have to think carefully about whether doing that would make sense.</p><p>More than that, and especially if you may consider saving the results ACROSS requests, it should be noted this sort of “lightweight, implicit caching” lacks various potentially useful features like limiting how many results should be saved, or implementing an eviction strategy, etc. At the point that could become an issue, you should really consider instead using the actual caching features, tags, and functions that CFML offers. :-)</p><p>None of this is to complain about the blog post or the memoization concept. Just sharing some additional thoughts.</p>
Comment by Charlie Arehart
2276 | September 04, 2019 02:31:05 PM GMT
underscore.cfc is good for stuff like this.   http://russplaysguitar.github.io/UnderscoreCF/
Comment by Jim Priest
2278 | September 05, 2019 01:35:28 PM GMT
<p>Hi Charlie, thanks for the comments – some good clarification in there <img src="https://s.w.org/images/core/emoji/2.3/72x72/1f642.png" alt="??" class="wp-smiley" style="height: 1em; max-height: 1em;" />Yeah, this is essentially achieving lightweight caching within the request. As you mention, CFML has it’s own caching functions and I’m not suggesting using this to replace them, it’s just another tool in the toolbox.</p><p>Hi Jim, I’ve looked at underscore.cfc before but hadn’t previously noticed that it already has a memoize function in it. That one is more sophisticated as you can pass in the hashFunction as well so if anyone wants to try out this kind of thing in real-world code, then they should definitely check that out. Thanks for mentioning it <img src="https://s.w.org/images/core/emoji/2.3/72x72/1f642.png" alt="??" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
Comment by aliaspooryorik
2279 | September 05, 2019 03:30:39 PM GMT
Cool Jim. I see it adds a memoize function, which may be what you were focused on for this comment. Otherwise, we should note that that library (adding lots of functional programming features to CFML) is from the CF10 timeframe, before CF added more and more such functional programming features itself in 11, 2016, and 2018. Indeed, it says it's compatible with CF10 and Railo 4 (the only versions listed), so it may be that some aspects of the lib may fail on later versions. Just clarifying, for readers.
Comment by Charlie Arehart
2280 | September 05, 2019 04:27:32 PM GMT
Good insights and considerations for sure. <img src="https://s.w.org/images/core/emoji/2.3/72x72/1f44c.png" alt="??" class="wp-smiley" style="height: 1em; max-height: 1em;" />
Comment by ahouseholder
4695 | March 20, 2020 11:20:54 PM GMT