Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Take last part from URL (Regex vs split)
(version: 0)
Comparing performance of:
Regex vs Split
Created:
7 years ago
by:
Guest
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
str.replace(/^(.*[\\\/])/, '');
Split
str.split('/').pop();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Regex
Split
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex
2417840.0 Ops/sec
Split
7390167.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**What is being tested** The provided JSON represents a JavaScript microbenchmark on the MeasureThat.net website, which compares two approaches to extract the last part from a URL: using regular expressions (`regex`) versus splitting the string by '/' (`split`). **Options compared** There are two options being compared: 1. **Regex**: Using the `replace()` method with a regular expression to remove the leading parts of the URL and keep only the last part. 2. **Split**: Using the `split()` method to split the URL into an array of parts and then taking the last element using `pop()`. **Pros and Cons** 1. **Regex**: * Pros: Can be more flexible and powerful for complex pattern matching, but may have performance overhead due to the complexity of the regex engine. * Cons: May not work as expected for certain input formats (e.g., URLs with special characters), can be slower than `split()` in many cases. 2. **Split**: * Pros: Typically faster and more efficient than regex, works well for simple URL formats, and is widely supported by browsers. * Cons: May not work correctly if the URL has multiple consecutive slashes or other edge cases. **Other considerations** * The `str` variable is initialized with a specific URL string in the Script Preparation Code section. This ensures that both test cases use the same input data. * There are no special JavaScript features or syntax being tested, so we can focus on the core functionality of each approach. * Other alternatives to achieve this task might include using other string manipulation methods (e.g., `substring()`, `indexOf()`), but `regex` and `split()` are two common and widely used approaches. **Library and its purpose** In this benchmark, no external library is being used. The `replace()` method uses the built-in JavaScript regex engine to perform regular expression matching, while the `split()` method uses a simple string splitting algorithm to divide the input string into parts. If we were using an external library for URL manipulation or parsing, it might involve additional dependencies and potential performance overhead.
Related benchmarks:
Get last part from URL (Regex vs Split vs Substring)
get last element of path: split vs regex
split vs regex onurl
regecxt
Comments
Confirm delete:
Do you really want to delete benchmark?