Actually I am getting skeptical about the performance gain claim. I wrote a little performance test using JMH - url-constraint-perf and the validation via the URL constructor comes out ahead. Using the single shot time mode, I get for example:
Note, that in this particular test regexp seems to e better for invalid urls, whereas URL constructor for valid ones. My guess is that building the throwing the exception for invalid URLs might take some time here. I also checked the URL implementation, the lookup/loading of the handler might take some time, but afaiks it is cached (which one would expect anyways). Even if I change the harness parameters, URL constructor validation seems to come out ahead.
Btw, I noticed some differences in what the regexp vs the URL constructor "consider" valid. I also looked at https://mathiasbynens.be/demo/url-regex and noticed that many of the URLs listed there as invalid would be accepted by the suggested regexp as well as the URL constructor. This is just showing what a mine field this whole url validation is.
|