Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
indexOf vs regex vs RegExp regexp fork 3
(version: 1)
Comparing performance of:
lowercase, indexof vs regex vs lowercase, includes
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var str = "This is a simple test!"; var needle = "simple"; var ri = /simple/i;
Tests:
lowercase, indexof
str.toLowerCase().indexOf(needle.toLowerCase()) > -1;
regex
var c = ri.test(str);
lowercase, includes
str.toLowerCase().includes(needle.toLowerCase());
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lowercase, indexof
regex
lowercase, 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):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmark test created on MeasureThat.net. The test compares the performance of three different approaches to search for a substring in a string: `indexOf` with case-insensitive mode, regular expressions (`regex`), and a forked version of the regular expression engine. **Approaches Compared** 1. **`toLowerCase()` + `indexOf()`**: This approach uses the `toLowerCase()` method to convert both the string and the search term to lowercase before searching for the substring. 2. **`var c = ri.test(str);` (regex)**: This approach uses a regular expression object (`ri`) to search for the substring in the string. The `test()` method returns `true` if the search term is found, and `false` otherwise. 3. **`str.includes(needle.toLowerCase())`**: This approach uses the `includes()` method with case-insensitive mode to search for the substring in the string. **Pros and Cons of Each Approach** 1. **`toLowerCase()` + `indexOf()`**: * Pros: Simple, widely supported, and fast. * Cons: May not be as efficient as other approaches for large strings or performance-critical applications. 2. **`var c = ri.test(str); (regex)`**: * Pros: Can handle more complex search patterns, faster than `indexOf()` for some cases. * Cons: Requires a regular expression engine, which can introduce additional overhead and complexity. 3. **`str.includes(needle.toLowerCase())`**: * Pros: Simple, fast, and widely supported. * Cons: May not be as efficient as other approaches for large strings or performance-critical applications. **Library Used** In the provided benchmark, a forked version of the regular expression engine is used. This suggests that the developer wanted to compare the performance of a custom regular expression implementation with the built-in `indexOf()` method and the `includes()` method. **Special JS Feature/Syntax** There are no special JavaScript features or syntaxes mentioned in the provided benchmark. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: * Using a library like `string-search` or `regex-perf` to compare the performance of different search algorithms. * Implementing a custom search algorithm using bitwise operations and loops. * Comparing the performance of different JavaScript engines or versions. Keep in mind that benchmarking JavaScript performance can be complex and requires careful consideration of factors like string length, search pattern complexity, and platform differences.
Related benchmarks:
index vs lastindexof startsWith
index vs lastindexof empty with startIndex set to 0
includes + toLowerCase vs RegExp + i
Matching regex with i flag vs lowercasing the string
Comments
Confirm delete:
Do you really want to delete benchmark?