var string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin eget tincidunt lacus, eget suscipit massa. Pellentesque tempor rutrum semper. Vestibulum eget lacus commodo, luctus libero quis, commodo sapien. Sed sagittis eu velit vitae dapibus. Donec viverra, sapien ac porta aliquet, risus est aliquet lectus, vitae maximus mauris diam vitae mi. Morbi in arcu lorem. Maecenas accumsan finibus varius. Nullam pellentesque id sem eu molestie. Sed ut urna risus. Donec venenatis at tellus vel posuere. Mauris vitae malesuada neque. Nullam bibendum maximus nisi. Praesent finibus porta risus, a rhoncus massa ultrices a. Aenean gravida justo at arcu sodales consectetur. Maecenas lobortis commodo nunc, sed tempor massa finibus non. Pellentesque a ante a lorem convallis commodo. Suspendisse potenti. Maecenas blandit eu arcu eget bibendum. Curabitur cursus quam eleifend fermentum molestie. Sed lacinia aliquam eleifend. Phasellus vel placerat velit, eu posuere tortor. Aenean varius mauris ut luctus faucibus. Nullam ut lacus in erat imperdiet venenatis. Duis suscipit nibh nec tristique tempus. Suspendisse nec lacus lacus. Pellentesque finibus pellentesque purus. Sed erat lorem, mollis ac metus a, volutpat facilisis lorem. Etiam venenatis commodo purus, vel dignissim felis sodales id. Curabitur ac volutpat nisi. Mauris ultricies odio nec enim tincidunt consequat non nec libero. Vestibulum faucibus elementum gravida. Curabitur vitae nibh quis leo gravida varius ut ac magna. Sed posuere feugiat dignissim. Integer nec auctor purus. Vestibulum ipsum neque, volutpat id nisl auctor, dictum interdum mi. Suspendisse commodo eros id pellentesque iaculis. Proin luctus purus elit, sit amet lobortis dolor lacinia vitae. Nullam lobortis sit amet nunc quis bibendum. Praesent ac nisl id lectus fermentum porta ut eget erat. Nullam vel ligula congue lectus faucibus gravida. Praesent condimentum semper orci, vitae maximus neque bibendum sit amet. Fusce pellentesque at felis eu sodales. Suspendisse augue.";
var regex = /tortor/;
regex.test(string);
string.includes("tortor");
string.match("tortor");
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
RegEx.test | |
String.includes | |
String.match |
Test name | Executions per second |
---|---|
RegEx.test | 1732790.9 Ops/sec |
String.includes | 15712028.0 Ops/sec |
String.match | 1523860.1 Ops/sec |
I'll break down the provided JSON and explain what's being tested, compared, and their pros and cons.
Benchmark Definition
The benchmark is designed to compare three different approaches for searching a string:
RegEx.test()
: A regular expression method to test if a string contains a specified pattern.string.includes()
: A built-in JavaScript method to check if a string includes another string or substring.string.match()
: Another regular expression method to find the first match of a pattern in a string.Script Preparation Code
The script preparation code defines:
string
containing some text with the word "tortor" multiple times.regex
that matches only the word "tortor".Html Preparation Code
There is no HTML preparation code provided, so this step can be skipped.
Individual Test Cases
The benchmark consists of three individual test cases:
RegEx.test()
: This test case runs the RegEx.test()
method on the prepared string with the regex
object.String.includes()
: This test case runs the includes()
method on the prepared string with the substring "tortor".String.match()
: This test case runs the match()
method on the prepared string with the regular expression regex
.Comparison
The benchmark compares the execution time of each test case across different browsers and devices:
Pros and Cons
Here's a brief analysis of the pros and cons for each approach:
RegEx.test()
:includes()
or match()
when dealing with complex patterns.string.includes()
:string.match()
:includes()
, but can capture multiple matches if needed.RegEx.test()
and may be less flexible.Other Considerations
When choosing between these approaches, consider the following factors:
Alternatives
Some alternative methods for string searching could include:
RegExp.prototype.test()
: Similar to RegEx.test()
, but with more flexibility in regular expression syntax.String.prototype.startsWith()
/String.prototype.endsWith()
: For prefix or suffix matching, respectively.\w+
) for simple word searches.Keep in mind that the best approach will depend on the specific requirements of your use case and performance constraints.