var str = "This is a simple test!";
var needle = "simple";
var ri = /simple/i;
str.toLowerCase().indexOf(needle.toLowerCase()) > -1;
var c = ri.test(str);
str.toLowerCase().includes(needle.toLowerCase());
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lowercase, indexof | |
regex | |
lowercase, includes |
Test name | Executions per second |
---|---|
lowercase, indexof | 10464964.0 Ops/sec |
regex | 10706055.0 Ops/sec |
lowercase, includes | 11004729.0 Ops/sec |
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
toLowerCase()
+ indexOf()
: This approach uses the toLowerCase()
method to convert both the string and the search term to lowercase before searching for the substring.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.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
toLowerCase()
+ indexOf()
:var c = ri.test(str); (regex)
:indexOf()
for some cases.str.includes(needle.toLowerCase())
: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:
string-search
or regex-perf
to compare the performance of different search algorithms.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.