Ad
  • Default User Avatar

    You're 100% correct. Don't know where I got the notion that A-z would match only letters. Thanks for the feedback! Should be A-Za-z instead.

  • Default User Avatar

    odaba, under the definition of this kata, var is a valid identifier (your solution seems to allows it too, so I don't know if you're criticizing or just pointing it out). Javascript, and most programming languages, have a blacklist of "reserved words" (e.g. var et al. in Javascript). Then there's the whole set of valid identifiers in Javascript, which has extensive Unicode support for identifiers, that this Kata doesn't come close to covering.

    OverZealous, in my tests, my solution is more efficient than the one you offered as an example of what would be more efficient, but it actually flips back and forth quite often. It really depends on the compiler because they're equivalent functionally (i.e. /[ab]/ == /a|b/). When I run the following in the Firefox console, b (the version I offered) has slightly better performance than a (the counterversion you're arguing has slightly better performance):

    var begin, end, i;
    var a = /^[A-z_$][$\w]*$/;
    var b = /^[A-z_$](?:\w|\$)*$/;
    
    begin = Date.now();
    for (i = 0; i < 1e6; i++) {
      a.test("abcdefghijklmnopqrstuvwxyz");
    }
    end = Date.now();
    console.log("a", end - begin);
    
    begin = Date.now();
    for (i = 0; i < 1e6; i++) {
      b.test("abcdefghijklmnopqrstuvwxyz");
    }
    end = Date.now();
    console.log("b", end - begin);
    
  • Custom User Avatar

    Also, using | instead of a character class ([$\w]) is going to have (slightly) poorer performance, since it will have to track location and backtrack on misses (and early exits are slower).

  • Custom User Avatar

    @odaba: Nope. At some point, your list runs out of elements.