Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex /i vs .includes with tolowerCase
(version: 0)
Testing some things
Comparing performance of:
Regex vs .includes
Created:
3 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; }
.includes
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]; window.match.toLowerCase().includes(str.toLowerCase()) x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Regex
.includes
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 what is being tested in the provided JSON. **Benchmark Purpose:** The benchmark compares the performance of two different string comparison methods: 1. **Regular Expressions (Regex)**: Specifically, the `/i` flag is used to make the regex match case-insensitive. 2. **String Inclusion with `toLowerCase()`**: This method converts both strings to lowercase before comparing them. **Options Compared:** The benchmark tests these two options against each other: * Regex (`window.regex`) * `.includes` with `toLowerCase()` **Pros and Cons of Each Approach:** ### Regular Expressions (Regex) Pros: * **Flexibility**: Regex can match complex patterns, including regular expressions. * **Powerful**: Regex provides a wide range of features for string manipulation. Cons: * **Performance Overhead**: Regex is generally slower than simple string comparison methods like `.includes`. * **Complexity**: Regex can be difficult to read and maintain due to its complexity. ### String Inclusion with `toLowerCase()` Pros: * **Speed**: `.includes` with `toLowerCase()` is typically faster than regex because it uses a simpler algorithm. * **Ease of Use**: This approach is straightforward and easy to understand. Cons: * **Limited Flexibility**: This method only compares strings based on their existence within a sequence, without additional features like pattern matching. **Library Used:** In this benchmark, the `window.regex` object represents a regex pattern. The `/i` flag makes it case-insensitive. **Special JS Feature/Syntax:** None are mentioned in the provided code or JSON. Now, let's talk about alternatives: 1. **String Matching Methods:** Other string comparison methods like `indexOf()`, `includes()`, and `localeCompare()` can be used for this type of benchmark. 2. **Regex Alternatives:** Other regex options like `test()` without the `/i` flag or using a different pattern altogether could be tested to see which performs better. In summary, the benchmark tests the performance of two string comparison methods: Regex and `.includes` with `toLowerCase()`. The Regex approach provides flexibility but incurs performance overhead due to its complexity. In contrast, `.includes` with `toLowerCase()` is faster but less flexible.
Related benchmarks:
Regex /i vs .indexOf with tolowerCase
Normalize path: JS Regex vs .endsWith vs .indexOf vs .slice
JavaScript Case Insensitive String Start: regex vs startsWith() vs indexOf() vs localeCompare()
Regex /i vs .indexOf with tolowerCase once
Comments
Confirm delete:
Do you really want to delete benchmark?