...

View Full Version : max length of eval expression



sudhakar_jr
01-12-2006, 09:05 AM
using eval function i am executing a lengthy string expression...in fact i am framing some string which contains the commands of creating elements using dom2...this is very very lengthy string

Is there any limitation for that

eval(exp);
here what is max length(limitation) of exp ?

thanks
Sudhakar

liorean
01-12-2006, 11:57 AM
No max length limit except for that placed on string length....

glenngv
01-13-2006, 05:01 AM
But why are you using eval in the first place? Can you post the code? I'm 99.9% sure eval is unnecessary.

brothercake
01-13-2006, 10:49 AM
If you're making an XMLHttpRequest and what your server returns is JavaScript, you have to eval() the response to get it to run.

Which is why I recommend not writing scripts that do this - don't return JS directly from the server, just return the data you need and process it in the original script.

That may not be the case in this case though - just making the point that this is a situation I'm aware of where it becomes 'necessary' to eval() a large amount of code.

martin_narg
01-15-2006, 09:53 PM
Even if the server returns javascript, you could still create a script element and append a textnode with the value of the ajax response. I've used this process in the past to remove the need to use eval on long strings, sure the script engine needs to process the remote output, but it's the same as dynamically including another js file.

Just an idea :D

m_n

Kor
01-16-2006, 12:28 PM
By the way... I am trying to find a reasonable way to avoid

eval('2+2')// or any other math oprations or stuff like that

Any ideeas?

brothercake
01-16-2006, 03:08 PM
By the way... I am trying to find a reasonable way to avoid

eval('2+2')// or any other math oprations or stuff like that

Any ideeas?
None that are any better, unless you can re-evaulate the format in which you're storing the original equation - why store it as a string, not (say) an array of numbers and operators?

brothercake
01-16-2006, 03:09 PM
Even if the server returns javascript, you could still create a script element and append a textnode with the value of the ajax response. I've used this process in the past
Does that work x-browser though? I may be out of date, but last time I tried anything like that it was strictly Moz/IE only..?

Kor
01-16-2006, 05:13 PM
None that are any better, unless you can re-evaulate the format in which you're storing the original equation - why store it as a string, not (say) an array of numbers and operators?
No, it's too intricate. It looks like this is a case in wich eval() is to be used...

jkd
01-16-2006, 05:24 PM
No, it's too intricate. It looks like this is a case in wich eval() is to be used...

I wrote an expression parser to do that sort of thing a while ago. I'm not entirely sure if it is significantly faster than running eval() (I needed it to generate an expression tree), but from an expression tree, it's really easy to evaluate the expression numerically.
http://www.jasonkarldavis.com/math/eval/test.xhtml
In retrospect, I never fully finished the script. Oh well.

Kor
01-16-2006, 05:29 PM
nice job... I'll study and might continue it, if time...


In retrospect, I never fully finished the script. Oh well.

There are fewer stars in the Universe than this kind of projects (started for fun or only to check some theoretical issues) I have started and left them somewhere an 3/4 from the end point... :D

Kor
01-16-2006, 05:38 PM
As a genuine Aries as I am, I started with a lot of enthusiasm many interesting projects, (several on the same time) and, when finding the general road to the end, get bored, let the project unfinished and looking for other new interesting affairs... Like the Englishmen say about Bridge. "It's a game where the Lords do the bid, while the Jacks do the rest of the play" (if you know Bridge, you will know what that means) :D I do need some Jacks from time to time...

liorean
01-16-2006, 05:49 PM
Kor: Have you tried messing around with regexes? If the expressions all take the look of (loperand operator roperand) you could create a regex that captures the three subpatterns. Then use a String.prototype.replace call with a callback function looking like this:
function(m,lod,op,rod){
return oOps[op](lod,rod);
}What is oOps? It's an object looking like this:
var
oOps={
'+':function(lod,rod){return (Number(lod)+Number(rod));},
'-':function(lod,rod){return (Number(lod)-Number(rod));},
'*':function(lod,rod){return (Number(lod)*Number(rod));},
'/':function(lod,rod){return (Number(lod)/Number(rod));},
'%':function(lod,rod){return (Number(lod)%Number(rod));},
'^':function(lod,rod){return Math.pow(Number(lod),Number(rod));}};

Kor
01-16-2006, 05:52 PM
hm... quite interesting approach. Never thought about that...

Kor
01-16-2006, 05:56 PM
Except that for a long expression I must return the result and recall the function with the next term and so on... If an intricate and long math expresion I have to do that as linear as possible. Not impossible, as long as anyway I am used to linearize everything :cool: .. Tx, interesting.

Kor
01-16-2006, 08:45 PM
But... does it worth for a long sequence of math operations? Wouldn't that solution eat more of the CPU energy that the simple eval()?

liorean
01-16-2006, 09:47 PM
If the length of the expression is relatively long, yes. The regex method needs to be done for all nested expressions, with a termination condition for when to stop replacing. Because regex cannot contain program calls or recursive definitions you need to write a rather large system of regex and function calls, essentially construct a form of grammar. Also, since the regex has no idea of operator precedence unless you separate each kind of operator into one replace by itself, it will need explicit parenthesis nesting or a rather complicated system for inserting that if you want to allow operator precedence rules.

However, note that eval is not always uncomplicated either. For example, NaN values propagate through the regex system because it uses Number calls to turn the input into numbers, while eval instead chokes on invalid input and throws an exception.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum