Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Check regex vs substring
(version: 0)
Comparing performance of:
regex current vs subString vs split vs regex complex
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var IDS = ["report_1", "report_1", "report_1", "report_1","report_1","report_1","report_1","report_1","report_1", "report_dsdsdssds_1", "report_dsdsdsds_1", "report_1", "report_1","report_1","report_1","report_1","report_1","report_1", "report_random_bla_foo_12321312", "report_random_bla_foo_12321312","report_random_bla_foo_12321312","report_random_bla_foo_12321312","report_random_bla_foo_12321312","report_random_bla_foo_12321312","report_random_bla_foo_12321312","report_random_bla_foo_12321312"];
Tests:
regex current
function getCollectionKey(key) { if (!key) { return ''; } return key.replace(/_\w+/g, '_'); } IDS.forEach((id) => getCollectionKey(id));
subString
function getCollectionKey(key) { if (!key) { return ''; } const lastUnderscoreIndex = key.lastIndexOf('_'); if (lastUnderscoreIndex === -1) { return key; } return key.substring(0, lastUnderscoreIndex); } IDS.forEach((id) => getCollectionKey(id));
split
function getCollectionKey(key) { if (!key) { return ''; } const segments = key.split('_'); if (segments.length > 1) { // Remove the last segment if it's a number or string (implies _foo_123 or _bar_) segments.pop(); } // Join the remaining segments back together with underscores return segments.join('_') + (key.endsWith('_') ? '_' : ''); } IDS.forEach((id) => getCollectionKey(id));
regex complex
function getCollectionKey(key) { if (!key) { return ''; } return key.replace(/_[^_]*$/, '_'); } IDS.forEach((id) => getCollectionKey(id));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
regex current
subString
split
regex complex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
regex current
644136.8 Ops/sec
subString
2239668.2 Ops/sec
split
583426.5 Ops/sec
regex complex
646120.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark and the different approaches being compared. **What is being tested?** The benchmark is testing four different ways to process a string of IDs, specifically designed to remove underscores from the IDs. The goal is to determine which approach is faster. **Approaches compared:** 1. **Regex (`regex current`):** Uses regular expressions to replace underscores with an empty string. 2. **Substring (`subString`):** Uses the `substring()` method to extract a part of the string, specifically everything before the last underscore. 3. **Split and join (`split`):** Splits the string into segments using underscores as separators, removes the last segment if it's a number or string, and then joins the remaining segments back together with underscores. 4. **Regex (complex)`:** Uses regular expressions to replace underscores from the end of the string. **Pros and Cons:** * **Regex (`regex current`):** Pros: + Simple to implement + Efficient for small strings Cons: + Can be slow for large strings due to the overhead of regular expression parsing + May not perform well on certain input formats (e.g., IDs with multiple consecutive underscores) * **Substring (`subString`):** Pros: + Fast and efficient, especially when dealing with long strings Cons: + May require more memory access patterns, leading to slower performance in some cases + Can be less accurate if the last underscore is not exactly at the end of the string * **Split and join (`split`):** Pros: + Can handle input formats with multiple consecutive underscores + Can be efficient when dealing with large strings, as it avoids the overhead of regular expression parsing Cons: + More complex implementation compared to regex or substring approaches + May require more memory access patterns, leading to slower performance in some cases * **Regex (complex)`:** Pros: + Efficiently handles input formats with multiple consecutive underscores + Avoids the need for substring extraction or splitting and joining operations Cons: + More complex regular expression pattern required + May be less readable and maintainable compared to simpler approaches **Library:** The `IDS` array is used as a test data set, but it's not a library in itself. It's an example of a dataset used to benchmark the processing speed of each approach. **Special JS feature or syntax:** None of the code snippets use special JavaScript features or syntax that would affect their performance significantly. **Other alternatives:** To compare these approaches, you could also consider: 1. Using other string manipulation methods, such as `replace()` with a custom replacement function. 2. Implementing the approach using a different programming language (e.g., C++ for high-performance processing). 3. Comparing the approaches on different platforms or devices to account for variations in performance. Keep in mind that the choice of approach depends on the specific requirements and constraints of your application, such as input format, performance requirements, and readability/maintainability considerations.
Related benchmarks:
RegEx.test vs. String.includes with array test
Regex vs No regex
array vs regex 2
substring vs substring+
Comments
Confirm delete:
Do you really want to delete benchmark?