mysticatea / regexpp

The regular expression parser for ECMAScript.
MIT License
153 stars 15 forks source link

Convert RegExp objects to Sets #8

Closed Validark closed 4 years ago

Validark commented 4 years ago

I think it is better to not rely on any RegExp library, especially for something as simple as detecting whether a string is in a given set of strings, or whether a given number is within a given set of numbers. Thus, I converted this library to use Set.has instead of RegExp.test.

(I would think Sets would be more efficient anyway, although it doesn't look like this library prioritizes efficiency in all places)

I am porting this library to Lua and this is one of the changes I had to make. I thought I might as well make it available for this version as well, if others could stand to benefit.

Validark commented 4 years ago

Side note: If the JIT engine can't make this optimization automatically, it would be optimally efficient to flatten the ranges arrays and iterate through them in pairs:

if (largeIdStartPatternSymbols.has(cp)) {
    return true
}

for (
    let i = 1, { length } = largeIdStartPatternRanges!;
    i < length;
    i += 2
) {
    if (
        largeIdStartPatternRanges![i - 1] <= cp &&
        cp <= largeIdStartPatternRanges![i]
    ) {
        return true
    }
}

return false
mysticatea commented 4 years ago

Thank you for taking this improvement. LGTM.