If ever a feature of JavaScript was considered harmful, it’s eval(). It’s so commonly abused that if I’m interviewing a JS web developer, I usually ask something along the lines of “what is eval(), and why shouldn’t you use it”. It’s so commonly abused that Yahoo JavaScript architect Douglas Cronkford considers it “evil”, and his JavaScript style checker JSLint reports use of it as an error.

Specifically, he says:

The eval function (and its relatives, Function, setTimeout, and setInterval) provide access to the JavaScript compiler. This is sometimes useful for parsing JSON text, but in virtually all other cases it indicates the presences of extremely bad coding. The eval function is the most misused feature of JavaScript.

Well I’m tired of my favourite function being badmouthed just because some people can’t program properly. In this article I show how to use eval to generate optimised code based on information only available at run time. The resulting code is 5 times faster than the non-generated version.