-
John Hawthorn authored
Running gsub! 5 times with string arguments seems to be faster than running it once with a regex and Hash. When there are matches to the regex (there are characters to escape) this is faster in part because CRuby will allocate a new match object and string as a key to lookup in the map hash provided. It's possible that could be optimized upstream, but at the moment this avoids those allocations. Surprisingly (at least to me) this is still much faster when there is no replacement needed: in my test ~3x faster on a short ~200 byte string, and ~5x faster on a pre-escaped ~600k twitter.json.
John Hawthorn authoredRunning gsub! 5 times with string arguments seems to be faster than running it once with a regex and Hash. When there are matches to the regex (there are characters to escape) this is faster in part because CRuby will allocate a new match object and string as a key to lookup in the map hash provided. It's possible that could be optimized upstream, but at the moment this avoids those allocations. Surprisingly (at least to me) this is still much faster when there is no replacement needed: in my test ~3x faster on a short ~200 byte string, and ~5x faster on a pre-escaped ~600k twitter.json.
Loading