Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Get last part from URL (Regex vs Split vs Substring)
(version: 1)
Comparing performance of:
Regex 1 vs Regex 2 vs Split vs Substring
Created:
7 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var str = 'https://www.testsite.com/some-path/another-path/2019/my-amazing-page-name.html';
Tests:
Regex 1
str.replace(/^(.*[\\\/])/, '');
Regex 2
str.replace(/([^\/]+)(?=[^\/]*\/?$)/, '');
Split
str.split('/').pop();
Substring
str.substring(str.lastIndexOf('/')+1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Regex 1
Regex 2
Split
Substring
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0
Browser/OS:
Chrome 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex 1
8969296.0 Ops/sec
Regex 2
274761.2 Ops/sec
Split
26167660.0 Ops/sec
Substring
33836904.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark tests the performance of three different approaches to extract the last part from a URL: using regular expressions (`Regex 1` and `Regex 2`), splitting by `/` (`Split`), and using the `substring()` method with `lastIndexOf()`. The benchmark aims to determine which approach is the fastest on the specified hardware configuration. **Benchmark Definitions** The three test cases are: * `Regex 1`: Uses a regular expression to remove everything before the first `/`, leaving only the last part of the URL. The pattern used is `^(.*[\\\\\\/])`, which matches any characters (`.`) followed by either a backslash or a forward slash (`\\` or `/`). The parentheses create a capture group, allowing us to extract the matched text. * `Regex 2`: Similar to `Regex 1`, but with an additional positive lookahead assertion (`(?=[^\\/]*\\/?$)`) to ensure that the extracted string does not contain any more forward slashes. This is done by checking if there are no more non-slash characters before or after the last forward slash. * `Split`: Splits the URL into substrings using `/` as the separator and returns the last part of the array using `pop()`. * `Substring`: Uses `lastIndexOf()` to find the position of the last occurrence of `/` in the URL and then extracts a substring starting from that index, effectively getting the last part. **Approach Comparison** 1. **Regular Expressions (Regex)**: * Pros: Flexibility and precision in pattern matching. * Cons: Can be slow due to the overhead of compiling regular expressions and executing complex patterns. In this case, `Regex 2` has a slight performance advantage over `Regex 1` likely because of the additional lookahead assertion which makes it less ambiguous for the engine. **Library Used**: JavaScript's built-in `String.prototype.replace()` method uses regular expressions under the hood. 2. **Split**: * Pros: Simple and straightforward approach that is easy to understand. * Cons: May not be suitable for complex URLs or cases where only a portion of the URL needs to be extracted. 3. **Substring**: * Pros: Efficient for simple substring extraction and may be faster than `Regex` due to fewer overheads. * Cons: Requires an additional operation (`lastIndexOf()`) to find the last occurrence of `/`, which can add to overall execution time. **Special Features** None mentioned in this explanation.
Related benchmarks:
Take last part from URL (Regex vs split)
get last element of path: split vs regex
split vs regex onurl
regecxt
Comments
Confirm delete:
Do you really want to delete benchmark?