var string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'
string.indexOf('ullamco')
string.includes('ullamco')
/ullamco/.test(string)
string == 'ullamco'
string === 'ullamco'
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
indexOf | |
includes | |
regex | |
Equal | |
Strict Equal |
Test name | Executions per second |
---|---|
indexOf | 380344416.0 Ops/sec |
includes | 11725476.0 Ops/sec |
regex | 424270048.0 Ops/sec |
Equal | 535005024.0 Ops/sec |
Strict Equal | 470092128.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
What is tested?
The provided JSON represents a benchmark that tests various string comparison methods in JavaScript:
string.includes('ullamco')
string.indexOf('ullamco')
/ullamco/.test(string)
string == 'ullamco'
string === 'ullamco'
These tests aim to compare the performance of different methods for finding a specific substring or character within a string.
Options compared
The benchmark compares five different options:
string.includes('ullamco')
: Uses the includes()
method, which returns true
if the specified value is found in the string.string.indexOf('ullamco')
: Uses the indexOf()
method, which returns the index of the first occurrence of the specified value in the string./ullamco/.test(string)
: Uses a regular expression (/ullamco/
) and the test()
method to match the substring against the entire string.string == 'ullamco'
: Uses the loose equality operator (==
) to compare the strings.string === 'ullamco'
: Uses the strict equality operator (===
) to compare the strings.Pros and Cons of each approach
Here's a brief overview of the pros and cons of each approach:
includes()
:indexOf()
internally.indexOf()
:-1
if not found, which can lead to errors if treated as a boolean value.test()
with regex:==
(loose equality):===
(strict equality):==
, but still has limitations due to type coercion.Library usage
The benchmark uses the string
object, which is a built-in JavaScript object that represents a string. The includes()
, indexOf()
, and test()
methods are all part of this object.
Special JS feature or syntax
There are no special features or syntax used in this benchmark beyond the standard JavaScript operators and methods. However, it's worth noting that some modern browsers support additional features like String.prototype.includes()
(which is similar to includes()
) and String.prototype.indexOf()
(which can be slower than the built-in indexOf()
method).
Alternatives
Other alternatives for string comparisons in JavaScript include:
Array.prototype.indexOf()
: Can be used to find substrings within an array.Regex.test()
with a flag: Can be used to perform precise matching using regular expressions, with flags like /g
or /m
to enable global or multiline matching.String.prototype.match()
: Returns an array of matches if the pattern is found in the string.These alternatives may offer different trade-offs in terms of performance, accuracy, and readability, depending on the specific use case.