On 03/04/2013 08:38 AM, Andrea Giammarchi wrote:
> I believe creating a redundant array of matches for no reason since these are 
> retrievable in any case through the RegExp constructor, considering exec and 
> match points to those RegExp properties anyhow, ain't needed when 
> re.test(value) is involved.

You're saving on array-construction and element-creation, but you're losing 
(with many to most current engines) on having to re-run the regular expression 
a second time to compute the match components.  You're almost certainly going 
to lose out overall when you take this extra cost into account.  Array and 
string creation are fast in engines these days.  Regular expression matching 
with the intent of constructing statics info is not.

Your code's also fragile against introduction of any other regular expressions 
within the code being demonstrated there.  If someone copies your code and 
tries to change the === to a function-based test, they might well change it to 
something that uses regular expressions.  That person might even be you: what's 
obvious now may not be obvious in the future.

> If re.test(value) RegExp.$1;
> 
> As easy as that: 1) is *not* a bad practice and 2) is less redundant than if 
> (re.test(value)) match = re.exec(value)[1];

To clarify the above, it's better to do:

var results = re.exec(value);
if (results)
  results[1];

> those properties I believe defined in the specs does not mean those 
> properties are bad (as I have just explained)

They are not defined in spec, partly because you're mistaken about them being a 
good idea.

> Also, that does not find a thing, that find an index ...

It's a fair point.  Although, given non-existence, it would seem to me you'd 
only want a method that returns an index, and then getting the element value is 
just arr[index] at that point.

> We are programmers, we find solutions/alternatives/optimizations ... right?

I'd also suggest we have the humility to recognize when we're trying to be too 
clever by half, as your forgotten "+" demonstrates.

Jeff
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to