tags:

views:

36

answers:

3

I could do it with regex (something like myString.match(/^[A-Za-z0-9]$/)) but it seems like an overkill.

What would be more effective performance-wise?

Thanks.

+4  A: 

Use the length property and the charCodeAt( 0 ) function to check the range of the ASCII value...

Macmade
+1  A: 

Well obviously, the most efficient is an indexed lookup into an array (pre-configured with appropriate boolean results). This is a very bad approach, in general, however. (Because it's non-standard and almost certainly not neccessary).

Noon Silk
Why regexp is an bad approach? I was thinking that would make the code more readable and easy to change the code when my criteria changes.. And why indexed lookup is better?
kvijayhari
@Vijay: It's better because it costs no time (or, to put it in 'Big Oh' it's, O(1). Lookup (table lookup) is the fastest way to do anything. Regexp is not a bad approach (I didn't say that, I was saying *my* suggested approach is "bad" (though it's the fastests)). Regexp is definitely the best approach in your specific case. Slow as it may be, it's clear, and scalable to a change in requirements. My method isn't. (easily). And I doubt this particular area will be your bottleneck (and if it is, there may be a better way to solve it, in general).
Noon Silk
@silky: Thanks for your patient reply.
kvijayhari
You're welcome :)
Noon Silk
+1  A: 

I would use regular expressions.

if(/^[a-z0-9]$/i.test(variable)) {}

edit: misread the question

dblackshell
(FWIW, incase you can't see the option, you can actually delete this response, [if you consider it useless]. It reduces noise, and saves you from downvotes :)
Noon Silk