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
llama3.1:latest
, generated one year ago):
Let's break down the benchmark test case. **Benchmark Name:** Regex vs indexOf+substring **Description:** The test case is designed to compare the performance of three different approaches for replacing multiple substrings in a string: 1. Using regular expressions (regex) 2. Using `indexOf` and `substring` methods 3. Using `split` and `join` methods **Test Case Preparation Code:** The code creates a sample string (`string`) with placeholders to be replaced, and an object (`replace`) that maps the placeholders to their corresponding replacement values. It then defines three functions: 1. `re_replace_all`: Uses a regex pattern to replace all occurrences of the specified substrings in the input string. 2. `s_replace_all`: Uses `indexOf` and `substring` methods to replace all occurrences of the specified substrings in the input string. 3. `s_replace_all2`: Uses `split` and `join` methods to replace all occurrences of the specified substrings in the input string. Each function is then called with the sample string, and the result is compared to an expected output using the `assertEqual` function. **Individual Test Cases:** The test cases are simply calls to each of the three functions: 1. "using regexp" 2. "using indexOf + substring" 3. "using split + join" **Latest Benchmark Result:** The benchmark results show the execution speed (ExecutionsPerSecond) for each test case on a specific browser version (Chrome 109): 1. Using `indexOf` and `substring` methods: approximately 1596319.625 executions per second 2. Using regular expressions: approximately 1168962.75 executions per second 3. Using `split` and `join` methods: approximately 1085018.0 executions per second **Analysis:** The test case is designed to compare the performance of three different approaches for replacing multiple substrings in a string. * The **regex** approach uses regular expressions, which can be efficient but may have performance issues if the regex pattern is complex or if the input string is very large. * The **indexOf+substring** approach uses `indexOf` and `substring` methods, which can be slower than regex but are more predictable in terms of performance. * The **split+join** approach uses `split` and `join` methods, which can be faster than `indexOf` and `substring` methods for large input strings. Based on the benchmark results, it appears that the **regex** approach is not significantly faster than the other two approaches, despite its potential efficiency benefits. The **indexOf+substring** approach performs slightly better than the **split+join** approach, which may be due to browser-specific optimizations or differences in string processing algorithms. Overall, this test case demonstrates the importance of considering performance trade-offs when choosing an algorithm for a specific task, and highlights the need for benchmarking and testing different approaches before making a decision.
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?