Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
EGO indexOf vs regex vs RegExp
(version: 2)
Comparing performance of:
lowercase, trim, indexof vs trim, lowercase, indexof vs regex vs lowercase, replace, indexof vs RegExp vs Same RegExp
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var str = "This is a simple test! Over 20 Distinct Floor Plans Modern Quartz Countertops Complete Stainless Steel GE Appliances Moen Fixtures Oval Soaking Tubs Washer and Dryer in Every Home Central Air and Heat Overside Balconies and Patios Fiber-Based Internet Speeds at 25/15, 50/25, and 100/50 (Faster Enterprise Speeds Available). Over 20 Distinct Floor Plans Modern Quartz Countertops Complete Stainless Steel GE Appliances Moen Fixtures Oval Soaking Tubs Washer and Dryer in Every Home Central Air and Heat Overside Balconies and Patios Fiber-Based Internet Speeds at 25/15, 50/25, and 100/50 (Faster Enterprise Speeds Available). Over 20 Distinct Floor Plans Modern Quartz Countertops Complete Stainless Steel GE Appliances Moen Fixtures Oval Soaking Tubs Washer and Dryer in Every Home Central Air and Heat Overside Balconies and Patios Fiber-Based Internet Speeds at 25/15, 50/25, and 100/50 (Faster Enterprise Speeds Available). Over 20 Distinct Floor Plans Modern Quartz Countertops Complete Stainless Steel GE Appliances Moen Fixtures Oval Soaking Tubs Washer and Dryer in Every Home Central Air and Heat Overside Balconies and Patios Fiber-Based Internet Speeds at 25/15, 50/25, and 100/50 (Faster Enterprise Speeds Available). Over 20 Distinct Floor Plans Modern Quartz Countertops Complete Stainless Steel GE Appliances Moen Fixtures Oval Soaking Tubs Washer and Dryer in Every Home Central Air and Heat Overside Balconies and Patios Fiber-Based Internet Speeds at 25/15, 50/25, and 100/50 (Faster Enterprise Speeds Available). Controlled Access Community Podium Lounge with Billiards Table Fitness Facility Professional Conference Room Business Center Private Screening Room Dog Park and Washing Station Secure and Reserved Parking Garage Valet Dry Cleaning This is a simple test! . Controlled Access Community Podium Lounge with Billiards Table Fitness Facility Professional Conference Room Business Center Private Screening Room Dog Park and Washing Station Secure and Reserved Parking Garage Valet Dry Cleaning This is a simple test! . Controlled Access Community Podium Lounge with Billiards Table Fitness Facility Professional Conference Room Business Center Private Screening Room Dog Park and Washing Station Secure and Reserved Parking Garage Valet Dry Cleaning This is a simple test! . Controlled Access Community Podium Lounge with Billiards Table Fitness Facility Professional Conference Room Business Center Private Screening Room Dog Park and Washing Station Secure and Reserved Parking Garage Valet Dry Cleaning On a very large Text string containing Community Amenities or Apartment Amenities"; var searchText = "community amenities"; var ri = new RegExp('community amenities', 'i');
Tests:
lowercase, trim, indexof
var a = str.toLowerCase().trim(); var b = searchText.toLowerCase().trim(); var c = a.indexOf(b) > -1;
trim, lowercase, indexof
var a = str.trim().toLowerCase(); var b = searchText.trim().toLowerCase(); var c = a.indexOf(b) > -1;
regex
var c = /simple/i.test(str);
lowercase, replace, indexof
var a = str.toLowerCase().replace(" ", ""); var b = searchText.toLowerCase().replace(" ", ""); var c = a.indexOf(b) > -1;
RegExp
var c = (new RegExp(searchText, 'i')).test(str);
Same RegExp
var d = ri.test(str)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
lowercase, trim, indexof
trim, lowercase, indexof
regex
lowercase, replace, indexof
RegExp
Same RegExp
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.1:latest
, generated one year ago):
Let's break down the benchmark test case. **What is being tested?** The test case compares four different approaches to finding a substring within a large string: 1. **indexOf()**: This is a built-in JavaScript method that returns the index of the first occurrence of a specified value (in this case, a string) in a string. 2. **Regular Expression (RegExp)**: This approach uses a regular expression to search for a pattern in a string. In this test case, it's used with the `test()` method to find a substring within the large string. 3. **Same RegExp**: This is an alternative implementation of the RegExp approach, using the same regular expression instance (`ri`) created earlier. 4. **toLowerCase() + trim() + indexOf()**: This approach first converts both strings to lowercase and trims any whitespace, then uses the `indexOf()` method to find a substring. **What are the options compared?** The test case compares the performance of these four approaches on finding a substring within a large string. **Pros and Cons:** * **indexOf():** + Pros: Simple and straightforward implementation. + Cons: May not be as efficient for large strings, as it has to scan the entire string from start to end. * **RegExp:** + Pros: Can be more efficient than `indexOf()` for large strings, especially when searching for a pattern. Also provides more flexibility with regular expressions. + Cons: Can be slower for simple substring searches due to the overhead of creating and compiling a regular expression. * **Same RegExp:** + Pros: Similar to RegExp, but uses an existing instance instead of re-creating one every time. + Cons: May not provide any significant performance gain over the original RegExp implementation. * **toLowerCase() + trim() + indexOf():** + Pros: Can be more efficient than `indexOf()` when the search string has a lot of whitespace or case sensitivity is not necessary. + Cons: Requires additional processing steps (lowercasing and trimming), which may slow down performance. **Latest benchmark result:** The test results show that: * The RegExp approach outperforms all other methods, with an execution rate of around 6.7 million per second. * The `toLowerCase() + trim() + indexOf()` method is the next fastest, with an execution rate of around 3-4 million per second. * The `indexOf()` and Same RegExp approaches trail behind, with execution rates of around 1-2 million per second. Keep in mind that these results are specific to this particular test case and may vary depending on your use case and environment.
Related benchmarks:
RegEx.exec vs String.match
RegEx.exec vs regex.test
RegEx.exec vs String.match (inline)
Comparing performance of: String.search vs String.match
RegEx.test vs. String.match vs. String.search
Comments
Confirm delete:
Do you really want to delete benchmark?