Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs indexOf+substring
(version: 0)
Comparing performance of:
using regexp vs using indexOf + substring vs using split + join
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
string = "some string with values in it and other value and many more values and we want to replace some strings in it" const replace = { 'value': 'X', 'and': 'or', 'string': 'number' } const expected = 'some number with Xs in it or other X or many more Xs or we want to replace some numbers in it' const re = /value|and|string/g const re_replace = (s) => replace[s] re_replace_all = (input) => input.replace(re, re_replace) s_replace_all = (input) => { let result = input for (let from in replace) { result = s_replace(result, from, replace[from]) } return result } const s_replace = (input, from, to) => { let start = '' let end = input let index = end.indexOf(from) while (index !== -1) { start += end.substring(0, index) + to end = end.substring(index + from.length) index = end.indexOf(from) } return start + end } s_replace_all2 = (input) => { let result = input for (let from in replace) { result = result.split(from).join(replace[from]) } return result } const assertEqual = (x, y) => { if (x !== y) throw new Error('nope') } assertEqual(re_replace_all(string), expected) assertEqual(s_replace_all(string), expected) assertEqual(s_replace_all2(string), expected)
Tests:
using regexp
re_replace_all(string)
using indexOf + substring
s_replace_all(string)
using split + join
s_replace_all2(string)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
using regexp
using indexOf + substring
using split + join
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares three different ways to perform text replacement within a string in JavaScript: **1. Using Regular Expressions (`re_replace_all`):** * **Description:** This approach uses a regular expression (`/value|and|string/g`) to match specific substrings ("value", "and", "string") within the input string. The `replace()` method then replaces each matched substring with the corresponding value from the `replace` object. * **Pros:** Regular expressions are powerful for complex pattern matching and can be quite efficient for repeated replacements. * **Cons:** Can sometimes be less readable than other approaches, especially for simple replacements. **2. Using `indexOf` and `substring` (`s_replace_all`):** * **Description:** This method iteratively searches the string for each target substring using `indexOf`. When found, it replaces the matched substring with its corresponding replacement value. * **Pros:** More straightforward to understand than regular expressions for simple replacements. * **Cons:** Can be less efficient than regular expressions, especially for many replacements or complex patterns. **3. Using `split` and `join` (`s_replace_all2`):** * **Description:** This approach splits the string at each target substring using `split()`. It then joins the resulting parts back together, replacing each matched substring with its corresponding replacement value. * **Pros:** Relatively simple to implement. * **Cons:** Can be inefficient for large strings or many replacements as it involves creating new arrays. **Other Considerations:** * **String Concatenation:** Be mindful of string concatenation performance, especially in loops. Consider using techniques like `StringBuilder` (for some languages) or efficient string manipulation libraries if you're dealing with many concatenations. * **Alternative Libraries:** For more complex text processing needs, explore dedicated JavaScript libraries like Lodash (`_.replace`), which often offer optimized solutions for various string manipulations. Let me know if you have any other questions.
Related benchmarks:
regex replace vs replaceAll vs replace in loop V1.1
endsWith slice vs Regex replace
test replace
replaceAll vs regex replace vs neu parser
Comments
Confirm delete:
Do you really want to delete benchmark?