Discussion on:

8
Comments

Join the conversation!

Follow via:
RSS
Email Alert
0 Votes
+ -
I was interested to read that the author's statement that the eval() function can have performance benefits.

My understanding is that it is one of the most ineffecient methods in JavaScript and I have therefore tried to avoid it all costs. I find that there is usually a simple way around using it.

Can anyone else clarify the performance implications of using this method?
0 Votes
+ -
You are correct... the eval() function carries more overhead than other JavaScript functions. For instance in 'Listing B' the author uses:

tempObj=EVAL("document.myForm." + fieldList);

There is no need for the eval function in this case. It would be better to use something like:

tempObj= document.myForm[fieldList];

...and save on the performance hit. Plus IMHO, the code readability suffers when using eval().
0 Votes
+ -
There are some specific cases - see the results in Table A in the article - where EVAL provides a faster solution.

I am not advocating that you should use EVAL all the time, but simply that in SOME cases it may be a more efficient solution.
0 Votes
+ -
Performance hit?
ntcse@... 23rd Mar 2004
I agree that the readability suffers with eval() and it should be avoided because of this, so as to use it for those situations where their is no comparable non-eval() equivalent.

As far as performance, most Javascript code does not need to be tuned to that level. Technically, eval() is slower than its straightforward equivalent. This is because the interpreter must evaluate twice: once to read the code containing the eval() and then when the call to eval() takes place. But we are talking about client-side script - this is done lightning fast compared to server-side requests. So unless we are talking about decent size libraries (1000 lines of code), I don't think this should be that much of an issue.
0 Votes
+ -
This applies to specifically to ASP for IIS but assuming other platforms ever implement a JavaScript server side language it should work.

What I do is send the name of a function via the QUERY_STRING to the ASP. For example,

http://localhost/mypage.asp?action=Save()&fname=Harry&lname=Belafonte

Then my ASP code would look like this:

<%
var action = String(Request("action"))
try
{
eval( action );
}
catch( e )
{
<><script>alert("No such function:<%=action<>\n\n<%=e.description<>");</script><%

}


function Save()
{
...
}

function Delete()
{
...
}

function List()
{
...
}
<>
Great article. I used what I learned here to build a dynamic select element that is updated from a mysql database.

I'd be happy to send you the code if you'd like to share it with your readers. Here is an example: http://danhagarty.com/ajax/index.php#db_example2

dan@danhagarty.com
0 Votes
+ -
In listing B you include the code:

tempObj=EVAL("document.myForm." + fieldList[j]);
if (tempObj.value.length==0)
{
alert(fieldList[j] + " requires a value");
}

This is really bad programming. It should instead be:

if (document.myForm.[fieldList[j]].value.length==0)
{
alert(fieldList[j] + " requires a value");
}
1 Vote
+ -
WRONG! I am a senior UX developer at monster.com. I have a great deal of understanding of JavaScript, and I can tell you that this article is dead wrong. In general, the only time to use eval is when consuming JSON data - other than that it's slower, less secure, less elegant, harder to debug, etc than other methods - OF WHICH THERE ARE! This author doesn't know JavaScript very well, obviously - the "ListingA" vs "ListingB" code samples is a joke. I could write ListingA to be just as short as ListingB by using hash-map syntax eg form["fieldID"] or document.getElementById w/ ids generated from the index. This article is bunk - disregard.
Keyboard Shortcuts:
Prev
Next
Toggle
Join the conversation
Formatting +
BB Codes - Note: HTML is not supported in forums
  • [b] Bold [/b]
  • [i] Italic [/i]
  • [u] Underline [/u]
  • [s] Strikethrough [/s]
  • [q] "Quote" [/q]
  • [ol][*] 1. Ordered List [/ol]
  • [ul][*] · Unordered List [/ul]
  • [pre] Preformat [/pre]
  • [quote] "Blockquote" [/quote]

Join the TechRepublic Community and join the conversation! Signing-up is free and quick, Do it now, we want to hear your opinion.