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
gemma2:9b
, generated one year ago):
This benchmark compares different approaches to finding the substring "community amenities" within a large text string. Here's a breakdown of each test case: **1. `lowercase, trim, indexof`:** * Converts the target string and the search string to lowercase using `toLowerCase()`. * Trims any leading or trailing whitespace from both strings. * Uses the `.indexOf()` method to find the starting position of the target substring within the text. **2. `trim, lowercase, indexof`:** * Similar to the previous approach but swaps the order of `toLowerCase()` and `.trim()`. This might seem insignificant, but JavaScript's optimization can sometimes be affected by the order of operations. **3. `lowercase, replace, indexof`:** * Replaces all occurrences of whitespace in the target string with a single space using `replace(/\s/g, ' ')`. * Then converts the target string to lowercase. * Uses `.indexOf()` to find the substring's position. **4. `RegExp`:** * Creates a new `RegExp` object with the target string and the "i" flag for case-insensitive matching. * Uses the `.test()` method of the `RegExp` object to check if the pattern exists within the text. **5. `Same RegExp`** * This test uses a pre-defined `RegExp` (ri) created earlier in the script. It directly applies this `RegExp` to the text using the `.test()` method, similar to the "RegExp" test. **6. `regex`:** * This is likely the simplest approach, directly using the JavaScript regular expression `/.+community.+amenities./i` to search for the substring within the text. The benchmark results show that using a pre-defined `RegExp` (`Same RegExp`) and the basic JavaScript regular expression (`regex`) are generally the fastest approaches in this particular scenario. **Alternatives & Considerations:** * **String Methods (split, join):** For simple string manipulations like replacing whitespace, you could explore alternatives like `.split()` and `.join()`. * **Libraries (like Lodash):** Libraries like Lodash offer optimized functions for common tasks like searching within strings. Let me know if you have any more questions about specific aspects of the benchmark!
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?