[thelist] [JS] regex function vs. plain function

Tom Dell'Aringa pixelmech at yahoo.com
Mon Apr 12 08:17:03 CDT 2004


--- Joshua Olson wrote:

> > Regex's are pretty expensive, so perhaps not.  But, I'd suggest
> > you look at performing this same check by using the built-in type

> > casting methods.
> > Simply trap the error (using try... catch) so that the method
> > doesn't exit out. If it errors when you try to convert to float
or 
> > int, then you know the value isn't valid.


--- Marcus Andersson <marcus at bristav.se> wrote:

> I wouldn't go that way since parseInt("123ab") would return 123 and
> wouldn't barf at the a and b.
> 
> I would go for reg exes if it reduces the amount of code (and
> therefore probably increases readability (that could be put under 
> discussion regarding reg exes though) and maintainability) unless
it 
> was inside a some loop and high performance is crucial.

It definitely reduces the amount of code which I am always for. I'm
not concerned with readability but performance. If I can deal with
the regex then our engineers can. They aren't inside any loop, they
are methods used by the validation object. (I'm going to post the
code soon for comments anyway, so you can all see it - and use it if
you want.)

> If it's just about going through some fields and checking them
> every now and then (ie before form submission and not on every key 
> stroke) I would surely go for reg exes. If you don't go the reg exp

> path due to performance in such a case I strongly suggest to study 
> the rules of optimization.

Right, before submission. I mean I've had it both ways and there is
certainly no noticeably different processing time - at least to the
human senses. I wonder if someone out there could explain the give
and take of regex vs. "plain old" processing. Regex is so much more
powerful, you would think that even if it grabs a bit more of
processing time, it saves by doing so much less?

Tom

=====
http://www.Pixelmech.com/ - read my latest blog posting!
http://www.DMXzone.com/ - JavaScript Author
http://SparklesParties.com - Princess parties for little girls!
http://www.thywordistruth.net/ - Eternal Life

"Well, my name's Dewey Oxburger. My friends call me Ox. I dont know if you've noticed, but I got a slight weight problem."





More information about the thelist mailing list