eval stuff (was Re: [thelist] testing for empty in javascript or vbscript)

.jeff jeff at members.evolt.org
Tue Jul 16 00:27:00 CDT 2002


shanx,

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> From: Shashank Tripathi
>
> > document.calcForm.elements['res' + x + y].value
>
> now you tell me! that does sound much simpler..
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

yes, very much so.

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> > i could understand the use of an eval() method in this
> > situation, but if you're using most any decent
> > server-side language you could have just as easily
> > written your own function
>
> why though..wouldn't an api function be faster than a
> custom function which would need to open a file and
> read the messages in, and then combine one of the
> lines with a variable?
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

any language that allows the creation of custom functions should be able to
execute them as fast or nearly as fast as an api function, with the
exception of the eval() function.  in the case of the eval() function the
operation that must be performed most assuredly would take longer than a
simple custom function that concatenated a passed argument to a string and
returned the new string.

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> in most decent languages, a file include is perhaps much
> faster than a file open/read..or perhaps i missed your
> idea?  time for a server-side lesson as well i guess.
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

or more like a lesson in how eval()/evaluate()/etc. actually work.  lemme
try to explain in as few words as possible.

you pass a string (whether as a result of concatenations directly in the
call or not) to the eval() method.  the eval() method then tries to find a
variable that matches the string you've passed it.  it also tries to see if
it recognizes the string as an expression, if it does, it executes that
expression.  that's extra work that you're placing on your system.  rather
than try to make it perform a conversion and then a lookup, why not just try
to access the variable or execute the expression in a more efficient manner.

here's a common use i've seen for the eval() method in javascript (all too
common and *very* unreadable).

styleProperty = 'backgroundColor';
styleValue = '#ffff00';
eval('document.body.style.'
    + styleProperty + ' = \''
    + styleValue + '\';');

so here's what the parser does.

1)  puts "backgroundColor" in place of the
    styleProperty variable in the string.
2)  puts "#ffff00" in place of the
    styleValue variable in the string.
3)  it has to figure out if it it's an
    expression or a variable.
4)  once it determines it's an expression
    it attempts to execute it causing the
    background color of the document to
    change to yellow.

using bracket notation it could have been as simple as:

styleProperty = 'backgroundColor';
styleValue = '#ffff00';
document.body.style[styleProperty] = styleValue;

now, all the parser has to do is do a simple lookup in the style object for
the "backgroundColor" property and assign the value of the "styleValue"
variable to it.  if it doesn't find the backgroundColor property in the
style object it simply creates it before assigning the value.

a very similar example, nearly the same, but hopefully shows how you're
unnecessarily making the parser work harder than it needs to.

resXY = eval('document.calcForm.res' + x + y);
resXY.value = 'foo';

1)  the parser has to perform the concatenation.
2)  then determine if the string is an expression
    or a variable.
3)  it sees that it's a reference to an object
    somewhere in memory (happens to be in the
    document usually) so it assigns the pointer
    to that object.
4)  you're now working with a pointer to an object
    and can then read/write the property values
    as necessary.

again, this could have been as simple as this.

document.calcForm.elements['res' + x + y].value = 'foo';

another example is for the execution of math.

x = 7;
y = 3;
eval(x + y);

guess what, that can be reduced to this.

x + y

as long as neither is typed as a string (by wrapping the variable values in
quotes) the system will perform addition rather than concatenation.  the
eval() is not needed and just forces the system to do extra work for
nothing.

does it make more sense now why the eval() method should be avoided whenever
possible?

.jeff

http://evolt.org/
jeff at members.evolt.org
http://members.evolt.org/jeff/







More information about the thelist mailing list