Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String matching - Regex stripping vs Concatenation of strings
(version: 3)
Comparing performance of:
Regex strip match vs Concatenate match string template vs Concatenate match with plus
Created:
2 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.stripHtmlSuffix = function(input) { return input.replace(/\.html$/, ''); } window.dataWithHTML = []; window.dataWithOutHTML = []; const possibleChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; const TOTAL_STRINGS = 1000; function getRandomInt(max) { return Math.floor(Math.random() * max); } function makeRandomString(len) { var text = ""; for (var i = 0; i < len; i++) { text += possibleChars.charAt(getRandomInt(possibleChars.length)); } return text; } for(let i=0;i<TOTAL_STRINGS;i++) { window.dataWithHTML.push( `${makeRandomString(100 + getRandomInt(150))}.html` ); window.dataWithOutHTML.push( makeRandomString(100 + getRandomInt(150)) ); }
Tests:
Regex strip match
let counter = 0; for(let i = 0;i < window.dataWithOutHTML.length; i++) { let dataWithOutHTML = window.dataWithOutHTML[i]; for(let j = 0;j < window.dataWithHTML.length; j++) { let dataWithHTML = window.dataWithHTML[j]; if (dataWithOutHTML === window.stripHtmlSuffix(dataWithHTML)) { counter++; } } }
Concatenate match string template
let counter = 0; for(let i = 0;i < window.dataWithOutHTML.length; i++) { let dataWithOutHTML = window.dataWithOutHTML[i]; for(let j = 0;j < window.dataWithHTML.length; j++) { let dataWithHTML = window.dataWithHTML[j]; if (dataWithHTML === `${dataWithOutHTML}.html`) { counter++; } } }
Concatenate match with plus
let counter = 0; for(let i = 0;i < window.dataWithOutHTML.length; i++) { let dataWithOutHTML = window.dataWithOutHTML[i]; for(let j = 0;j < window.dataWithHTML.length; j++) { let dataWithHTML = window.dataWithHTML[j]; if (dataWithHTML === (dataWithOutHTML + '.html')) { counter++; } } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Regex strip match
Concatenate match string template
Concatenate match with plus
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex strip match
4.1 Ops/sec
Concatenate match string template
6.9 Ops/sec
Concatenate match with plus
6.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided benchmark compares three different approaches for string matching in JavaScript: 1. **Regex stripping**: This approach uses the `replace()` method with a regular expression to remove the `.html` suffix from each string. The regular expression `/\\.html$/` matches any string that ends with `.html`. 2. **Concatenation of strings**: This approach concatenates the original string with ".html" using template literals (`${dataWithOutHTML}.html`) or the `+` operator (`dataWithOutHTML + '.html'`). The idea is to compare the concatenated string with the original string without the suffix. 3. **Concatenation with plus**: Similar to the previous approach, but uses the `+` operator instead of template literals. **Pros and Cons:** * **Regex stripping**: + Pros: Efficient and concise way to remove a specific suffix from a string. + Cons: May be slower than other approaches for large strings or when dealing with edge cases (e.g., strings that end with multiple `.html` suffixes). * **Concatenation of strings**: This approach can lead to performance issues due to the creation of new strings and the overhead of concatenation. However, it's a simple and straightforward way to compare the original string with the concatenated one. * **Concatenation with plus**: Similar to the previous approach, but using the `+` operator might be slightly slower than template literals. **Other considerations:** * The benchmark uses a large dataset of random strings (`TOTAL_STRINGS = 1000`) to ensure that the results are representative for most use cases. * The test cases focus on finding exact matches between the original string and one of the processed strings, which might not be suitable for all scenarios (e.g., fuzzy matching or partial matches). * The benchmark doesn't consider other factors that might affect performance, such as string encoding, character set, or cultural variations. **Library usage:** There are no libraries explicitly mentioned in the benchmark code. However, some JavaScript features and syntax are used: * **Template literals**: Used to concatenate strings in a readable way. * **Regular expressions**: Used to remove the `.html` suffix from strings. * **String concatenation with `+` operator**: Used as an alternative approach for concatenating strings. **Special JS features or syntax:** The benchmark uses some advanced JavaScript features and syntax: * **Arrow functions**: Used in the `makeRandomString()` function to define a simple function expression. * **Spread syntax**: Used to create arrays (`window.dataWithOutHTML` and `window.dataWithHTML`) from scratch. * **Template literals**: Used to concatenate strings in a readable way. **Alternatives:** Other approaches for string matching could include: * Using a dedicated library like `regex-stripping` or `string-strip` * Implementing custom substring extraction functions * Utilizing specialized data structures like suffix trees or Tries
Related benchmarks:
Regex vs .startsWith
Regex vs .startsWith 2
Normalize path: JS Regex vs .endsWith vs .indexOf vs .slice
Regex vs .indexOf vs .includes
Comments
Confirm delete:
Do you really want to delete benchmark?