tc39 / proposal-regex-escaping

Proposal for investigating RegExp escaping for the ECMAScript standard
http://tc39.es/proposal-regex-escaping/
Creative Commons Zero v1.0 Universal
363 stars 32 forks source link

Polyfills #82

Closed lionel-rowe closed 3 weeks ago

lionel-rowe commented 3 weeks ago

Super happy to see this at Stage 3! 🎉

I noticed there's a polyfill under development at es-shims/RegExp.escape and available via NPM, and I've also made my own lightweight one: lionel-rowe/regexp-escape-polyfill. I'll probably add it to JSR and/or NPM at some point, but for now it's just a GitHub repo.

Benchmarks/comparison:

Name Time (avg) Iter/s Bundle size (minified + gzipped) Details
lionel-rowe/regexp-escape-polyfill 1.301 µs 768,315.5 498 B Modern JS/ESM only, with TS support; aims for full compatibility with the spec (verified via test262) but may differ on implementation details.
es-shims/RegExp.escape 11.189 µs 89,373.5 7.1 kB Aims for ES3 compliance; written with CJS modules; more closely follows the implementation details of the algorithm in the spec.
zloirock/core-js 1.567 µs 637,988.6 5.29 kB CoreJS implementation; ES5 (?) support including various other polyfills; written with CJS modules

Anyway, feel free to add this info to the README etc.

zloirock commented 3 weeks ago

https://github.com/zloirock/core-js#regexp-escaping

lionel-rowe commented 3 weeks ago

Thanks @zloirock ! Added that to the table

zloirock commented 3 weeks ago

I don't know about perf, but the bundle size for core-js(-pure)/actual/regexp/escape definitely not 88kB -) https://bundlejs.com/?q=core-js-pure%2Factual%2Fregexp%2Fescape&treeshake=%5B*%5D https://bundlejs.com/?q=core-js%2Factual%2Fregexp%2Fescape&treeshake=%5B*%5D

lionel-rowe commented 3 weeks ago

I don't know about perf, but the bundle size for core-js(-pure)/actual/regexp/escape definitely not 88kB -) https://bundlejs.com/?q=core-js-pure%2Factual%2Fregexp%2Fescape&treeshake=%5B*%5D https://bundlejs.com/?q=core-js%2Factual%2Fregexp%2Fescape&treeshake=%5B*%5D

Gotta admit I was a little perplexed at how high the numbers looked for es-shims/RegExp.escape and zloirock/core-js. Methodology I'm using is fetching the ESM version via esm.sh then bundling and minifying with esbuild (which in theory should tree shake correctly for ESM bundling). No gzip. Still, 12.8 kB is a long way off of 88 kB, so I guess either esbuild isn't tree shaking properly, esm.sh is adding a massive amount of cruft, or there's a bug in my code. Edit: I've replaced with min+gzipped numbers from bundlejs.com (and bundlephobia in the case of es-shims/RegExp.escape as I can't get it to load properly via any of the CDNs available on bundlejs.com).

ljharb commented 3 weeks ago

(Computing bundle size numbers absent a complete application is always going to be inaccurate due to optimizations and deduplication, and displaying them makes it really easy for people to draw inaccurate conclusions in both directions.)

Thanks! I'll add es-shims and core-js to the readme; when you've published yours to npm, I'd love a PR to add it as well.