Archive for April, 2006

One-Line JavaScript Memoization

Sunday, April 16th, 2006

Computing the length of a Bezier curve is expensive, but the length of a given Bezier doesn’t change over time. In my JavaScript Bezier implementation, I wanted to compute the length only the first time it’s need, and save this result in order to return instantly thereafter.

This is a special case of memoization. There are well-known strategies for implementing memoization. But getLength is a nullary function, and there’s a trick for implementing memoization of nullary methods in a dynamic language such as JavaScript (or Python or Ruby). In these languages, you can memoize a nullary method by adding one line to it, without any support libraries. This line replaces the method by a constant function, that returns the computed value. This memoization strategy is also more efficient than the general-purpose solution that non-nullary methods require.


Read the rest of this entry »

Wide URLs with WideURL.com

Saturday, April 1st, 2006

For years now, I’ve been a great fan of TinyURL.com. That web site allows you to create a short representation of a longer URL, for use in email.

One of the problems with those URLs, though, and with URLs in general, is that they’re misleadingly short. A particular web page may have a lot of significance, but if it doesn’t take up much of your message, there’s just no way for the recipient to see this at a glance.

WideURL.com fixes this. It creates an URL with more visual impact.

For example, here’s the WideURL for this post: http://wideurl.com/aitch-tee-tee-pea-colon-double-slash-oh-ess-tee-double-ee-ell-ee-dot-see-oh-em-slash-aye-are-see-aitch-eye-vee-ee-ess-slash-two-double-zero-six-slash-zero-four-slash-doubleyou-eye-dee-ee-you-are-ell.


Read the rest of this entry »