Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf vs .startsWith vs .substr kasdfladjkf
(version: 0)
Testing some things
Comparing performance of:
Regex vs .includes
Created:
3 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; }
.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 (2)
Previous results
Fork
Test case name
Result
Regex
.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):
**Overview** The provided benchmark, hosted on MeasureThat.net, aims to compare the performance of different string searching methods in JavaScript: regular expressions (`regex`), the `.indexOf()` method, and the `.startsWith()` method. **Benchmark Definitions** There are two main benchmark definitions: 1. **Regex**: This test case uses a regular expression to search for the pattern `test` within an array of randomly generated strings. The script prepares the data by generating 100,000 random strings and storing them in the `window.data` property. 2. **.includes**: This test case uses the `.includes()` method to check if a given string is present in an array of random strings. **Options Compared** The benchmark compares the following options: * **Regex**: The built-in JavaScript regular expression engine * **.indexOf()**: A method that returns the index of the first occurrence of a substring within a string * **.startsWith()**: A method that returns `true` if the string starts with a given prefix **Pros and Cons** Here's a brief analysis of each approach: * **Regex**: + Pros: Highly flexible, can match complex patterns, and is widely supported. + Cons: Can be slow for large strings or datasets due to its complexity and overhead. * **.indexOf()**: + Pros: Fast and efficient for small to medium-sized strings, as it only needs to scan the string once. + Cons: May not be suitable for larger datasets or complex patterns. * **.startsWith()**: + Pros: Similar to `.indexOf()`, but optimized for checking if a string starts with a specific prefix. + Cons: May not be as flexible as `regex` or as efficient as `.indexOf()` for large strings. **Libraries and Special Features** In this benchmark, there is no explicit mention of any third-party libraries or special features. However, it's worth noting that the JavaScript engine used by MeasureThat.net may have its own optimizations or quirks that could affect the results. **Other Alternatives** If you're interested in exploring alternative string searching methods, here are a few options: * **Spline**: A library that provides a faster and more efficient way to search for patterns in strings. * **StringSearch**: A lightweight library that offers a simple and fast way to perform substring searches. * **Boyerkas Algorithm**: A heuristic algorithm that can be used to find the first occurrence of a substring in a string. Keep in mind that these alternatives may not be as widely supported or well-maintained as the built-in JavaScript methods.
Related benchmarks:
Regex vs .indexOf vs .startsWith 2
Normalize path: JS Regex vs .endsWith vs .indexOf vs .slice
Regex vs .indexOf vs .includes
JavaScript Case Insensitive String Start: regex vs startsWith() vs indexOf() vs localeCompare()
Comments
Confirm delete:
Do you really want to delete benchmark?