Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Javascript: Case insensitive string comparison indexOf vs includes 2
(version: 0)
Comparing performance of:
toLowerCase_includes vs Regex_indexOf
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var words = Array.from({ length: 1000 }, (_, i) => `word${i + 1}`); var searchString = "WORD512"; var searchStringRegex = new RegExp(searchString, 'i');
Tests:
toLowerCase_includes
const filteredCase1 = words.filter(word => word.toLowerCase().includes(searchString.toLowerCase()));
Regex_indexOf
const filteredCase2 = words.filter(word => searchStringRegex.test(word));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
toLowerCase_includes
Regex_indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
toLowerCase_includes
31322.2 Ops/sec
Regex_indexOf
39338.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmarking scenario: **Benchmark Definition** The benchmark is designed to compare two approaches for case-insensitive string comparison in JavaScript: `indexOf` with regular expressions and `includes` method. **Script Preparation Code** The script preparation code creates an array of 1000 words, each word being a sequence of "word" followed by a number from 1 to 1000. It also sets up two variables: * `searchString`: a string "WORD512" * `searchStringRegex`: a regular expression created from `searchString` with the 'i' flag (case-insensitive) The purpose of this script preparation code is to provide a consistent and controlled environment for the benchmarking test cases. **Html Preparation Code** There is no HTML preparation code provided, which suggests that this benchmark is focused on JavaScript performance alone. **Individual Test Cases** There are two individual test cases: 1. **toLowerCase_includes**: This test case uses the `filter()` method with a callback function that checks if a word includes the lowercase version of the search string (`searchString.toLowerCase()`) using the `includes()` method. 2. **Regex_indexOf**: This test case uses the `filter()` method with a callback function that checks if a word matches the regular expression created from the search string (`searchStringRegex`) for case-insensitive matching. **Library and Purpose** In both test cases, the `Array.prototype.filter()` method is used to filter the array of words. The `includes()` method is also used in one of the test cases. These are built-in JavaScript methods that do not require any external libraries. There are no special JavaScript features or syntax mentioned in this benchmark. **Options Compared** The two options being compared are: 1. **toLowerCase_includes**: uses the `filter()` method with a callback function and the `includes()` method. 2. **Regex_indexOf**: uses the `filter()` method with a callback function and a regular expression for case-insensitive matching. **Pros and Cons of Each Approach** Here's a brief overview of each approach: 1. **toLowerCase_includes**: * Pros: simple, easy to read, and understand. * Cons: may be slower than using a regular expression due to the overhead of calling `toLowerCase()` on each string. 2. **Regex_indexOf**: * Pros: can provide more flexible matching options, such as matching an empty string or using flags like 'g' for global matching. * Cons: may be less readable and maintainable than using a simple callback function, especially for complex patterns. **Other Considerations** The benchmarking scenario assumes that the `filter()` method is optimized for performance, which might not always be the case in production code. Additionally, the use of `includes()` method might depend on the specific requirements of the application, such as finding multiple occurrences of a substring. In terms of alternatives, there are other ways to perform string matching in JavaScript, such as using a library like `indexOf` or `match`, but these options are not explicitly mentioned in this benchmark.
Related benchmarks:
case insensitive search
includes + toLowerCase vs RegExp + i
Case insensitive RegEx.test vs. String.includes when string doesn’t match
Case insensitive search: regex.test() vs string.toLowerCase().includes()
Comments
Confirm delete:
Do you really want to delete benchmark?