var string = '{{ vars.test asflkglsfsdfm;l; s;lfkg;sldf g;sd f; sd;fl ;lsd fg;lsd f;gdsfg s df;gl sdl;fgkl;sdkf gl;s df;gl s;dlfg;sdfg }}';
var startDelim = '{{';
var endDelim = '}}';
var matcher = new RegExp(`${startDelim}((?!${endDelim}).+?)${endDelim}`, 'g');
string.includes(startDelim) && string.includes(endDelim);
string.matchAll(matcher);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
String.includes() | |
String.matchAll() |
Test name | Executions per second |
---|---|
String.includes() | 3746454.0 Ops/sec |
String.matchAll() | 4001735.2 Ops/sec |
Benchmark Explanation
The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net, titled "String.includes() vs. String.matchAll v2". This benchmark compares the performance of two string searching methods in JavaScript:
String.includes()
: A method that searches for a specified value (substring) within a string and returns a boolean indicating whether the value is found.String.matchAll(v2)
: A method introduced in ECMAScript 2022, which uses regular expressions to search for all occurrences of a pattern within a string. The method returns an iterator yielding match results.Comparison Options
The benchmark compares the performance of these two methods:
String.includes()
String.matchAll(v2)
Pros and Cons
String.includes()
:
Pros:
Cons:
String.matchAll(v2)
:
Pros:
Cons:
matchAll()
Library and Special JS Feature
The benchmark uses the String.matchAll()
method, which is a part of the JavaScript language itself. No external libraries are required.
There are no special JavaScript features or syntax used in this benchmark beyond what's standard in modern JavaScript.
Alternative Approaches
Other alternatives to these two methods include:
String.prototype.indexOf()
instead of String.includes()
text-search
or regex-compiler
for optimized performanceKeep in mind that these alternatives may not provide the same level of browser support or simplicity as the standard methods used in this benchmark.