Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex /i vs .indexOf with tolowerCase once
(version: 0)
Testing some things
Comparing performance of:
Regex vs .indexOf
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regex = /test/i; window.match = 'test'; var data = window.data = []; const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var TOTAL_STRINGS = window.TOTAL_STRINGS = 100000; function getRandomInt(max) { return Math.floor(Math.random() * max); } function makeRandomString(len) { var text = ""; for( var i=0; i < len; i++ ) { text += possible.charAt(getRandomInt(possible.length)); } return text; } while (data.length < TOTAL_STRINGS) { data.push(makeRandomString(getRandomInt(20))); }
Tests:
Regex
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var regex = window.regex; while (x < TOTAL_STRINGS) { const str = data[x]; regex.test(str); x += 1; }
.indexOf
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.match; while (x < TOTAL_STRINGS) { const str = data[x]; str.toLowerCase().indexOf(match) > -1; x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Regex
.indexOf
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 break down the provided JSON and explain what is tested on that website. **Benchmark Definition** The benchmark defines two test cases: 1. **Regex**: This test case creates a regular expression (`window.regex`) with the pattern `/test/i` (case-insensitive) and then checks if the string `match` matches this pattern using the `test()` method. 2. **.indexOf**: This test case creates a regular expression object (`window.match`) with the value `"test"` and then uses the `toLowerCase()` method to convert the strings in the array `data` to lowercase, followed by checking if the resulting string contains the matched substring using the `indexOf()` method. **Options Compared** The two options being compared are: * **Regex**: Using a regular expression object with the `test()` method to check for a pattern. * **.indexOf**: Using the `toLowerCase()` method to convert strings to lowercase and then checking if the resulting string contains a substring using the `indexOf()` method. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Regex**: + Pros: Can be used for more complex pattern matching, can handle multiple patterns, and is often faster than `.indexOf()`. + Cons: Can be slower due to the overhead of compiling and executing regular expressions, and may have performance issues with very large inputs. * **.indexOf**: + Pros: Often faster than `Regex` due to its simple and lightweight nature, can handle large inputs without significant performance degradation. + Cons: May not be as flexible or powerful as `Regex`, requires conversion of strings to lowercase for case-insensitive matching. **Library and Purpose** In this benchmark, there are two libraries being used: * **RegExp**: A built-in JavaScript library that provides support for regular expressions. The RegExp object is used in the Regex test case. * **String.prototype.toLowerCase()**: A built-in JavaScript method that converts a string to lowercase. This method is used in both test cases. **Special JS Feature or Syntax** There is no special JS feature or syntax being tested in this benchmark, other than the use of regular expressions and string methods like `toLowerCase()` and `indexOf()`. **Other Alternatives** Other alternatives for searching strings include: * **String.prototype.indexOf()**: This method checks if a substring exists at a specified position in a string. * **Array.prototype.includes()**: This method checks if an element is present in an array. * **Regular expression arrays**: Some modern browsers support using regular expressions as arrays, which can be used to search for patterns. Keep in mind that these alternatives may have different performance characteristics and use cases compared to the approaches tested in this benchmark.
Related benchmarks:
Regex vs .indexOf vs .startsWith 2
Regex /i vs .indexOf with tolowerCase
Regex vs .indexOf vs .includes
JavaScript Case Insensitive String Start: regex vs startsWith() vs indexOf() vs localeCompare()
Comments
Confirm delete:
Do you really want to delete benchmark?