Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf vs .includes
(version: 0)
Testing some things
Comparing performance of:
Regex vs .indexOf vs .includes
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regex = /^test/; window.match = 'test'; var data = window.data = []; const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var TOTAL_STRINGS = window.TOTAL_STRINGS = 100000; function getRandomInt(max) { return Math.floor(Math.random() * max); } function makeRandomString(len) { var text = ""; for( var i=0; i < len; i++ ) { text += possible.charAt(getRandomInt(possible.length)); } return text; } while (data.length < TOTAL_STRINGS) { data.push(makeRandomString(getRandomInt(20))); }
Tests:
Regex
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var regex = window.regex; while (x < TOTAL_STRINGS) { const str = data[x]; regex.test(str); x += 1; }
.indexOf
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.match; while (x < TOTAL_STRINGS) { const str = data[x]; str.indexOf(match) === 0; x += 1; }
.includes
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.match; while (x < TOTAL_STRINGS) { const str = data[x]; str.includes(match); x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Regex
.indexOf
.includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex
877.7 Ops/sec
.indexOf
21938.5 Ops/sec
.includes
22024.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of different JavaScript methods for string matching is a crucial task, and MeasureThat.net provides a valuable platform for benchmarking. The provided JSON represents a benchmark test that compares three popular JavaScript string matching methods: 1. **Regex** (Regular Expressions) 2. **.indexOf** (String.prototype.indexOf()) 3. **.includes** (String.prototype.includes()) Let's dive into the details of each approach: **Regex** Regex is a powerful pattern-matching method that allows you to search for patterns in strings. It's widely used in many programming languages, including JavaScript. Pros: * Highly flexible and customizable * Can be used for complex string matching tasks Cons: * Performance can be slow due to the complexity of the regular expression engine * May not perform well on very large datasets or performance-critical applications **.indexOf** IndexOf is a simple method that finds the index of a specific value within a string. Pros: * Fast and efficient, especially for small to medium-sized strings * Works well for finding exact matches Cons: * Limited flexibility and customization options * May not work correctly for very large datasets or edge cases **.includes** Includes is similar to .indexOf but returns `true` if the value is found anywhere in the string. Pros: * Slightly faster than .indexOf due to reduced overhead * Works well for finding exact matches, similar to .indexOf Cons: * May not be as efficient as .indexOf for very large datasets Now, let's consider other alternatives that could have been used instead of these three methods: 1. **String.prototype.search()**: This method returns the index of the first occurrence of a value within a string. 2. **String.prototype.match()**: This method returns an array containing all matches of a regular expression pattern in a string. 3. **NativeArray.prototype.indexOf()** or **NativeArray.prototype.lastIndexOf()**: These methods are similar to .indexOf but work on native arrays instead of strings. In this benchmark, the test users special JS features and syntax, such as regular expressions (Regex). The `window.regex` variable is defined in the Script Preparation Code section, which allows the tests to use it throughout the benchmark.
Related benchmarks:
Regex vs .indexOf vs .startsWith
Regex vs .indexOf vs .startsWith 2
JavaScript Case Insensitive String Start: regex vs startsWith() vs indexOf() vs localeCompare()
Regex /i vs .indexOf with tolowerCase once
Comments
Confirm delete:
Do you really want to delete benchmark?