var strings = [
'googlebot',
'mediapartners-google',
'adsbot-google',
'googleweblight',
'storebot-google',
'google-pagerenderer',
'bingbot',
'bingpreview',
'slurp',
'duckduckbot',
'baiduspider',
'yandex',
'sogou',
'linkedinbot',
'bitlybot',
'tumblr',
'vkshare',
'quora link preview',
'facebookexternalhit',
'facebookcatalog',
'twitterbot',
'applebot',
'redditbot',
'slackbot',
'discordbot',
'whatsapp',
'skypeuripreview',
'ia_archiver',
]
var regex = /Googlebot|Mediapartners-Google|AdsBot-Google|googleweblight|Storebot-Google|Google-PageRenderer|Bingbot|BingPreview|Slurp|DuckDuckBot|baiduspider|yandex|sogou|LinkedInBot|bitlybot|tumblr|vkShare|quora link preview|facebookexternalhit|facebookcatalog|Twitterbot|applebot|redditbot|Slackbot|Discordbot|WhatsApp|SkypeUriPreview|ia_archiver/i;
var input = "Hello, World!"
regex.test(input);
strings.includes(input.toLowerCase());
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
RegEx.test | |
String.includes (with lowercase) |
Test name | Executions per second |
---|---|
RegEx.test | 21283938.0 Ops/sec |
String.includes (with lowercase) | 23266736.0 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Definition
The website is testing three different approaches to check if a string contains a specific substring: regex.test()
, String.includes()
, and String.match()
with case insensitivity.
Options Compared
regex.test()
: This method uses regular expressions to search for the specified pattern in the input string. The i
flag at the end of the regex ensures case-insensitive matching.String.includes()
: This method checks if a substring is present within another string, ignoring case by default (it's converted to lowercase).String.match()
with case insensitivity: This method returns an array containing the matched text, and it can be made case-insensitive by using the i
flag.Pros and Cons of Each Approach
regex.test()
:String.includes()
:String.match()
with case insensitivity:String.includes()
, and it returns an array of matches (even if only one is found).Library Usage
None.
Special JS Features or Syntax
The benchmark uses JavaScript's built-in string methods (regex.test()
, String.includes()
, String.match()
), which are part of the ECMAScript standard. No special features or syntax are required for this benchmark.
Other Alternatives
indexOf()
: This method can be used to search for a substring in a string, but it's not case-insensitive by default.Localspace RegExp
: This is a native JavaScript implementation of regular expressions, which might offer better performance than the built-in regex.test()
method.In summary, this benchmark tests three approaches to perform a simple string search: regex.test()
, String.includes()
, and String.match()
with case insensitivity. Each approach has its pros and cons, and understanding these trade-offs is essential for choosing the right method for your specific use case.