[Javascript] Good vs. Eval (was Form validation)

Jeff Greenberg jeff at websciences.org
Wed Jan 21 11:48:04 CST 2004


>From: javascript-bounces at LaTech.edu On Behalf Of Håkan Magnusson
>
>Of course, another argument would be that eval is insanely heavy on the 
>parser,
>

This is true for evaluating expressions and statements, etc.  But one of 
the places where eval can really shine is in dynamic function 
generation/modification.  Here I am talking specifically about either 
creating functions from scratch, or modifying functions based on certain 
criteria or input (to do so, you can get at the code of an existing 
function by using its toString() method).

In this case, eval is actually quite fast because it is doing nothing 
more or less than what the Javascript parser has to do when it first 
encounters your script-- converting the text source of a function to a 
cached internal representation of the script. From there on out, of 
course, the generated functions run like any other code. This is in 
contrast to how eval is normally misused for evaluating individual 
statements or series of statements over and over again. In that case, it 
is like you are loading a new little script into the Javascript parser 
every time you use eval.

One example of dynamic code generation in JS is a recent library I 
created to memoize (cache) function results to speed up complex or 
recursive functions. Although I eventually ended up using a different 
tactic (only because the toString() method for functions is broken in 
Opera 7.23, which I needed to support), dynamic code generation created 
the fastest, most elegant output.  Basically the goal was for users of 
the library to be able to create their functions as they normally would, 
then pass them to a memoize() function that would return the modified 
function.  My library does this now, and the code to do so is actually 
smaller than if I had used code generation, but the modified code is not 
quite as fast and is certainly not nearly as elegant. Any time I want to 
add functionality, I have to come up with various workarounds and 
sometimes strange ways of accomplishing my goals, which adds to the 
complexity of the library and could create future maintenance problems. 
Adding functionality for the code generating version was trivial.

Granted, dynamic code generation doesn't usually come up unless you are 
trying to create things like optimizers, extensions to the JS core 
language, etc., but my guess is that it could be applied to other 
situations as well. In particular, it probably could be useful when 
speed is critical, but functionality of complex prototypes or functions 
may need to be changed. By generating code directly instead of using an  
inheritance chain, or wrapping existing code within other functions, you 
can get as much speed as is feasible in any given environment.

That was all rather abstract and long, but I hope I was communicating 
effectively.

-JG




More information about the Javascript mailing list