var strUrl = 'https://website.com/?blahRef=766f2afedcc311ea3333a6ba9e46292aae400001&sdtid=14265950&sdop=1&sdpid=139549172&sdfid=9&lno=1&trd=https%20www%20othersite%20com%20p%20germ%20x%20o%20&pv=&au=&sdttt=jjj%7Crecombbb%7C25b08e67-1a86-44e1-aae9-73920b9b44c2&u2=https%3A%2F%2Fwww.website.com%2Fp%2Fgerm-x-original-hand-sanitizer-32-fl-oz%2F-%2FA-797633334642';
strUrl.match(/(?:.+)(?=http)(.+)/)[1];
strUrl.replace(/(?:.+)(?=http)/g,'');
var arrStringSplitLen = strUrl.split('http');
'http'+arrStringSplitLen[arrStringSplitLen.length-1];
'http'+strUrl.split('http').pop();
strUrl.substr(strUrl.lastIndexOf('http'));
strUrl.slice(strUrl.lastIndexOf('http'));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Match RegEx via Lookahead and Capture Group | |
Replace Unwanted Parts with Replace RegEx | |
String Split and Reference Array Index with length -1 (const intermediate) | |
String Split and Reference Array Index with length -1 (var intermediate) | |
String Split and Reference Array Index with length -1 (no intermediate var) | |
String Split with pop |
Test name | Executions per second |
---|---|
Match RegEx via Lookahead and Capture Group | 1620662.8 Ops/sec |
Replace Unwanted Parts with Replace RegEx | 103375.3 Ops/sec |
String Split and Reference Array Index with length -1 (const intermediate) | 3298802.0 Ops/sec |
String Split and Reference Array Index with length -1 (var intermediate) | 3140513.5 Ops/sec |
String Split and Reference Array Index with length -1 (no intermediate var) | 3305088.2 Ops/sec |
String Split with pop | 3310777.5 Ops/sec |
Let's break down the provided JSON and explain what is being tested, compared options, their pros and cons, and other considerations.
Benchmark Definition
The benchmark definition is a string that represents the code to be executed. In this case, it's a JavaScript expression that extracts part of a long URL string using different methods.
Options Compared
There are six different methods compared in the benchmark:
strUrl.match(/(?:.+)(?=http)(.+)/)[1]
: Uses regular expressions to extract the HTTP part of the URL.strUrl.replace(/(?:.+)(?=http)/g,'')
: Uses the replace()
method with a regular expression to remove the non-HTTP parts of the URL.var arrStringSplitLen = strUrl.split('http');\r\n'http'+arrStringSplitLen[arrStringSplitLen.length-1]
: Splits the URL into an array and then uses array indexing to extract the HTTP part.'http'+strUrl.split('http').pop();
: Similar to option 3, but uses string concatenation instead of array indexing.strUrl.substr(strUrl.lastIndexOf('http'));
: Uses the substr()
method to extract the last occurrence of 'http' in the URL.strUrl.slice(strUrl.lastIndexOf('http'));
: Uses the slice()
method to extract a subset of characters from the URL starting at the last occurrence of 'http'.Pros and Cons
Each option has its own pros and cons:
Other Considerations
match()
method is more efficient than replace()
, but only when used correctly. In this case, it's using a lookahead and capture group, which may not be optimal.Libraries
There are no specific libraries mentioned in the benchmark definition.
Special JS Features or Syntax
The benchmark uses special JavaScript features and syntax, including:
split()
, substr()
, and slice()
methodsThese features and syntax are not explicitly explained in the benchmark result, but they can be found in various online resources or documentation.
Alternatives
Other alternatives for extracting part of a URL string include: