const testStrFirst = 'foo';
'foobar'.includes(testStrFirst);
const testStrSecond = 'bar';
'foobar'.includes(testStrSecond);
const testStrNotMatch = 'baz';
'foobar'.includes(testStrNotMatch);
const testStrFirst = 'foobar';
testStrFirst.match('foo');
const testStrSecond = 'foobar';
testStrSecond.match('bar');
const testStrNotMatch = 'foobar';
testStrNotMatch.match('baz');
const testStrFirst = 'foo';
testStrFirst.match(/^(foo|bar)$/);
const testStrSecond = 'bar';
testStrSecond.match(/^(foo|bar)$/);
const testStrNotMatch = 'baz';
testStrNotMatch.match(/^(foo|bar)$/);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
String.includes | |
String.match needle | |
String.match regexp |
Test name | Executions per second |
---|---|
String.includes | 829334528.0 Ops/sec |
String.match needle | 1496076.9 Ops/sec |
String.match regexp | 7238416.5 Ops/sec |
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents a benchmark test case that compares the performance of three different approaches for matching a string against more than one possibility: String.includes
, String.match
with a needle, and String.match
with a regular expression.
Test Cases
The benchmark consists of three individual test cases:
includes()
is compared to itself for different strings.match()
when passed a single string (needle) against another string.match()
when passed a regular expression against a string.Options Compared
The benchmark compares three different options:
String.includes
: A simple method to check if a substring is present in a string.String.match(needle)
: A method that searches for a specific pattern (needle) in a string and returns an array of matches or null if no match is found.String.match(regexp)
: A method that uses regular expressions to search for patterns in a string.Pros and Cons
Here's a brief overview of the pros and cons of each approach:
String.includes
:String.match(needle)
:includes()
.includes()
and has limitations on the type of patterns that can be used (e.g., no regex support).String.match(regexp)
:includes()
or match(needle)
, and may have performance issues with very long strings.Library and Purpose
In the provided benchmark, no libraries are explicitly mentioned. However, it's worth noting that both String.match()
and String.includes()
methods rely on internal JavaScript engine optimizations, which can vary between browsers and versions.
Special JS Features or Syntax
None of the test cases explicitly use special JavaScript features or syntax (e.g., ES6 classes, async/await, etc.). The tests focus on the performance comparison of three basic string matching approaches.
Alternatives
If you're looking for alternatives to MeasureThat.net, some popular options include:
These alternatives offer various features, such as support for asynchronous tests, caching, and more advanced analysis tools.