Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Demo benchmark
(version: 0)
From https://benchmarkjs.com/
Comparing performance of:
RegExp#test vs String#indexOf vs String#match vs String#includes
Created:
9 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div>This is demo</div>
Script Preparation code:
console.log("js preparation");
Tests:
RegExp#test
/o/.test('Hello World!');
String#indexOf
'Hello World!'.indexOf('o') > -1;
String#match
!!'Hello World!'.match(/o/);
String#includes
'Hello World!'.includes('o')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
RegExp#test
String#indexOf
String#match
String#includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll provide an in-depth explanation of the benchmark test cases, options compared, pros and cons, and other considerations. **Benchmark Test Cases** The provided JSON contains four individual test cases: 1. `RegExp#test`: Tests the `test()` method of regular expressions. 2. `String#indexOf`: Tests the `indexOf()` method of strings. 3. `String#match`: Tests the `match()` method of strings with a regular expression pattern. 4. `String#includes`: Tests the `includes()` method of strings. **Options Compared** The options compared in these test cases are: * `RegExp#test`: + Old syntax: `/o/.test('Hello World!');` + New syntax (recommended): `'Hello World!'.match(/o/)` or `'Hello World!'.includes('o')` * `String#indexOf`: + Old syntax: `indexOf('o') > -1;` + No direct equivalent in modern JavaScript * `String#match`: + Old syntax: `!!'Hello World!'.match(/o/);` (note the doublebang `!!`) + New syntax (recommended): `'Hello World!'.includes('o')` **Pros and Cons** Here are some pros and cons of each option: * `RegExp#test`: + Old syntax: - Pros: Still supported in older browsers. - Cons: Less readable and less efficient than the new syntax. + New syntax (recommended): - Pros: More concise, easier to read, and more efficient. - Cons: Requires support for modern JavaScript features (modern browsers only). * `String#indexOf`: + No direct equivalent in modern JavaScript. However, most strings already have a built-in method called `includes()` or a similar function that achieves the same result. * `String#match`: + Old syntax: - Pros: Still supported in older browsers. - Cons: More complex and less efficient than the new syntax. + New syntax (recommended): - Pros: More concise, easier to read, and more efficient. - Cons: Requires support for modern JavaScript features (modern browsers only). **Other Considerations** * **Modern JavaScript Features**: The new syntax (`includes()` and `match()`) requires support for modern JavaScript features like template literals, arrow functions, or `let`/`const` declarations. This means that older browsers may not support these features. * **Browser Compatibility**: The benchmark test cases assume that the benchmarking library supports running on a variety of devices and browsers. However, some options (like `RegExp#test`) might require specific browser versions or configurations to work correctly. **Alternatives** If you need to support older browsers or want alternative approaches for these string comparison methods, consider the following: * For strings: + Use the `indexOf()` method with a negative index check (`-1`). + Use the `includes()` method (available in most modern JavaScript engines since ECMAScript 2015). + Implement a custom string search function using loop constructs. * For regular expressions: + Use the `test()` method with regular expression patterns. + Use a library like jQuery or Underscore.js that provides built-in support for regular expressions. In summary, the new syntax (`includes()` and `match()`) is more concise, readable, and efficient. However, it requires support for modern JavaScript features and may not work in older browsers.
Related benchmarks:
querySelectorAll vs getElementsByTagName iteration
GBCR Sequential Intense
testtesttesttesttest
querySelectorAll vs getElementsByTagName iteration 2
Arrays Destructuring
Comments
Confirm delete:
Do you really want to delete benchmark?