var str = '';
var i = 0;
while (i <= 1E5) str += i++;
str += 'end';
const item = str.includes('end');
const index = str.indexOf('end');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
String.prototype.includes | |
String.prototype.indexOf |
Test name | Executions per second |
---|---|
String.prototype.includes | 500115808.0 Ops/sec |
String.prototype.indexOf | 503111488.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
What is tested?
The benchmark measures the performance difference between two string searching methods in JavaScript: includes()
and indexOf()
. The test case creates a large string by concatenating numbers from 0 to 100,000, followed by the word "end". This allows the benchmark to evaluate the efficiency of these string searching methods under various conditions.
Options compared
Two options are being compared:
true
if the string contains the specified value, and false
otherwise. It is typically used with substrings or values that can be present in a string without necessarily matching it exactly.-1
if not found.Pros and cons
indexOf()
for these use cases.includes()
when an exact match is expected.Library usage
There is no specific library used in this benchmark. The two methods are part of the JavaScript Standard Library.
Special JS features or syntax
None are mentioned in the provided benchmark definition.
Other considerations
When interpreting these results, consider the following:
includes()
and indexOf()
, which may not be a critical concern for most use cases.Alternative approaches
If you're looking to create your own benchmark or optimize the performance of these methods in a specific scenario, consider the following alternatives:
jsstrlib
or fast-string-search
provide optimized implementations for string searching methods.Keep in mind that the best approach depends on the specific requirements of your use case and performance characteristics of your target environment.