'Is it better to regexp from start or from substring
I have a very big string (several Mb) I have to search in using regexp in a loop, advancing from string elements to string elements. For example, searching A, then B fellowing A, then 5 following B, etc :
[...]A[...]B[...]5[...]
The logical way would be to search for A, then search B from A, then 5 from B, etc. But that method, in JS, require to substring at every loop (as find/search/match/exec don't permit to search from a specific position).
My question, in that regard, is quite simple: is it better (in term of speed) to work with a "temporary string" which will be subtringed at every loop:
let tmpString = stringToParse
loop 0..n:
match = tmpString.find()
tmpString = tmpString.substring(match.index)
resulting on a big string copy (even if a little smaller at each iteration) in memory, or rather regex from the start every time:
loop 0..n:
match = stringToParse.find()
but resulting here, of course, of a parsing of string parts which doesn't need to be parsed anymore.
Solution 1:[1]
Write your algorithms, then go to https://jsbench.me/ and check which one is faster.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | djcaesar9114 |
