var str = 'https://www.testsite.com/some-path/another-path/2019/my-amazing-page-name.html';
str.replace(/^(.*[\\\/])/, '');
str.split('/').pop();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Regex | |
Split |
Test name | Executions per second |
---|---|
Regex | 2417840.0 Ops/sec |
Split | 7390167.5 Ops/sec |
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:
replace()
method with a regular expression to remove the leading parts of the URL and keep only the last part.split()
method to split the URL into an array of parts and then taking the last element using pop()
.Pros and Cons
split()
in many cases.Other considerations
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.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.