Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For vs indexof
(version: 5)
Comparing performance of:
for loop vs indexof ubstring test vs Regex
Created:
9 years ago
by:
Registered User
Jump to the latest result
Tests:
for loop
var splitParameters = "=&=&=&to=&cc=&bcc=&subjectbox=&composeid=1476889432826&from=robebsta%40gmail.com&subject=&draft=157dd78731699d77&bwd=&rm=&cans=&ctok=&ac=&adc=&ishtml=1&nowrap=0&body=%3Cdiv%20dir%3D%22ltr%22%3E%3Cdiv%20class%3D%22gmail_default%22%20style%3D%22font-family%3Aarial%2Chelvetica%2Csans-serif%22%3Easdasdasdffbbfffsdfdsfggjjjjtrset%3C%2Fdiv%3E%3C%2Fdiv%3E&pte=&pti=&bpfs=&uet=&pbgt=&pbgas=&pbgir=&msg_encrypt=0&signature=0&"; splitParameters = splitParameters.split("&"); for (var i = 0; i < splitParameters.length; i++) { var bodyContentIteration = splitParameters[i]; if (bodyContentIteration.indexOf("draft=") > -1) { return bodyContentIteration.replace("draft=",""); } }
indexof ubstring test
var bodyContent = "=&=&=&to=&cc=&bcc=&subjectbox=&composeid=1476889432826&from=robebsta%40gmail.com&subject=&draft=157dd78731699d77&bwd=&rm=&cans=&ctok=&ac=&adc=&ishtml=1&nowrap=0&body=%3Cdiv%20dir%3D%22ltr%22%3E%3Cdiv%20class%3D%22gmail_default%22%20style%3D%22font-family%3Aarial%2Chelvetica%2Csans-serif%22%3Easdasdasdffbbfffsdfdsfggjjjjtrset%3C%2Fdiv%3E%3C%2Fdiv%3E&pte=&pti=&bpfs=&uet=&pbgt=&pbgas=&pbgir=&msg_encrypt=0&signature=0&"; startIndex = bodyContent.indexOf("&draft="); if (startIndex > -1) { var endIndex = bodyContent.indexOf("&", startIndex + 1); return bodyContent.substring(startIndex + 7, endIndex); }
Regex
var regex = /&draft=(.*?)&/g; var regexMatch = regex.exec("=&=&=&to=&cc=&bcc=&subjectbox=&composeid=1476889432826&from=robebsta%40gmail.com&subject=&draft=157dd78731699d77&bwd=&rm=&cans=&ctok=&ac=&adc=&ishtml=1&nowrap=0&body=%3Cdiv%20dir%3D%22ltr%22%3E%3Cdiv%20class%3D%22gmail_default%22%20style%3D%22font-family%3Aarial%2Chelvetica%2Csans-serif%22%3Easdasdasdffbbfffsdfdsfggjjjjtrset%3C%2Fdiv%3E%3C%2Fdiv%3E&pte=&pti=&bpfs=&uet=&pbgt=&pbgas=&pbgir=&msg_encrypt=0&signature=0&"); if (regexMatch != null && regexMatch.length > 1) { var messageId = regexMatch[1]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for loop
indexof ubstring test
Regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the benchmark. **Benchmark Definition** The benchmark is defined by a JSON object that specifies three main components: 1. `Script Preparation Code`: This section contains code that prepares the test script for execution. In this case, it's empty. 2. `Html Preparation Code`: This section contains HTML code that sets up the environment for the test. Again, it's empty in this example. 3. `Benchmark Definition`: This is the actual benchmark definition, which specifies the logic to be tested. In this case, there is only one benchmark definition provided: "var bodyContent = \"=&=&=&to=&cc=&bcc=&subjectbox=&composeid=1476889432826&from=robebsta%40gmail.com&subject=&draft=157dd78731699d77&bwd=&rm=&cans=&ctok=&ac=&adc=&ishtml=1&nowrap=0&body=%3Cdiv%20dir%3D%22ltr%22%3E%3Cdiv%20class%3D%22gmail_default%22%20style%3D%22font-family%3Aarial%2Chelvetica%2Csans-serif%22%3Easdasdasdffbbfffsdfdsfggjjjjtrset%3C%2Fdiv%3E%3C%2Fdiv%3E&pte=&pti=&bpfs=&uet=&pbgt=&pbgas=&pbgir=&msg_encrypt=0&signature=0&\";\r\n startIndex = bodyContent.indexOf(\"&draft=\");\r\n if (startIndex > -1) {\r\n var endIndex = bodyContent.indexOf(\"&\", startIndex + 1);\r\n return bodyContent.substring(startIndex + 7, endIndex);\r\n }" This benchmark definition contains a simple string manipulation test that extracts the value of the `draft` parameter from a URL. **Options Compared** There are three options being compared in this benchmark: 1. **For loop**: The first option uses a traditional `for` loop to iterate over an array. 2. **IndexOf ubstring test**: The second option uses the `indexOf()` method with a regular expression to find the index of the `draft` parameter in the URL string. 3. **Regex**: The third option uses a regular expression (`/&draft=(.*?)&/g`) to extract the value of the `draft` parameter from the URL string. **Pros and Cons** Here are some pros and cons of each approach: * **For loop**: Pros: straightforward, easy to understand; Cons: may be slower due to the overhead of loop control. * **IndexOf ubstring test**: Pros: simple, efficient; Cons: assumes the `draft` parameter is always at a specific index in the URL string. * **Regex**: Pros: flexible, powerful; Cons: can be slow and resource-intensive if used incorrectly. **Other Considerations** * The benchmark uses a Windows-specific URL format, which may not be representative of other platforms or browsers. * The `draft` parameter is hardcoded in the URL string, which may not be realistic in all scenarios. * The regular expression used to extract the `draft` parameter is simple and efficient, but may not cover all possible variations. **Latest Benchmark Result** The latest benchmark result shows that: * **Regex**: 980088 executions per second * **IndexOf ubstring test**: 3700696 executions per second * **For loop**: 4299475 executions per second These results suggest that the `for` loop is the fastest option, followed closely by the `IndexOf ubstring test`. The `Regex` option is slower due to its resource-intensive nature.
Related benchmarks:
index vs lastindexof empty with startIndex set to 0
JavaScript search() vs indexOf()
String.indexOf vs String.indexOf with the second parameter
String.indexOf(char) vs String.indexOf(char, position)
.includes() vs indexOf() for single-character search in string
Comments
Confirm delete:
Do you really want to delete benchmark?