Faster javascript string replace
Monkey Forums/Monkey Programming/Faster javascript string replace
| ||
Hello, I've been playing around with the string replace function in an attempt to speed it up, I've found much faster ways (especially on larger datasets like the source of a webpage). The following is a test (HTML page) for three methods (Monkey's, regex and a string split/join combo). I get good results for all three browsers for the two new functions, split/join seems to be best for FF and Chrome: My results: 5000 iterations (small data) IE9: Monkey: 24 regex: 4 Split/Join: 10 Chrome: Monkey: 32 regex: 15 Split/Join: 9 FF4: Monkey: 23 regex: 22 Split/Join: 10 500 iterations, large data IE9: Monkey: 6210 regex: 13 Split/Join: 28 Chrome: Monkey: 1718 regex: 27 Split/Join: 23 FF4: Monkey: 19903 regex: 28 Split/Join: 25 I realise that this isn't a real-world usage example, but I can see situations where you might for instance want to scrape data from an XML or RSS feed, and string replacements may well be necessary. |
| ||
Hi, The problem with the regex one is when there are regex chars in the find string - I was a bit wary about the overhead of having to 'escape' the find string. But I think the split/join version is genius! |
| ||
I hadn't considered the possibility of regex chars in the find string. In which case, split/join wins. :) |