Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Comare ^Regex vs startsWith, .search vs .indexOf, .test vs .include (spelled correctly)
(version: 0)
Testing some things
Comparing performance of:
^test Regex vs .startsWith vs .search(RegExp) vs .indexOf vs RegExp # test vs .includes
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.matchStartRegex = /^test/; window.matchRegex = /test/; window.matchSubstr = '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:
^test Regex
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var regex = window.matchStartRegex; let result = false; while (x < TOTAL_STRINGS) { const str = data[x]; result = regex.test(str); x += 1; }
.startsWith
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.matchSubstr; let result = false; while (x < TOTAL_STRINGS) { const str = data[x]; result = str.startsWith(match); x += 1; }
.search(RegExp)
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var regex = window.matchRegex; let result = false; while (x < TOTAL_STRINGS) { const str = data[x]; result = str.search(regex); x += 1; }
.indexOf
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.matchSubstr; let result = -1; while (x < TOTAL_STRINGS) { const str = data[x]; result = str.indexOf(match); x += 1; }
RegExp # test
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var regex = window.matchRegex; let result = false; while (x < TOTAL_STRINGS) { const str = data[x]; result = regex.test(str); x += 1; }
.includes
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.matchSubstr; let result = false; while (x < TOTAL_STRINGS) { const str = data[x]; result = str.includes(match); x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
^test Regex
.startsWith
.search(RegExp)
.indexOf
RegExp # test
.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 break down the benchmark and explain what's being tested, the pros and cons of each approach, and other considerations. **Benchmark Overview** The benchmark tests four different string matching methods in JavaScript: 1. `^test Regex` (starts with) 2. `.startsWith` 3. `.search(RegExp)` 4. `.indexOf` These methods are compared to measure their performance differences. **Options Compared** Here's a brief explanation of each method and its purpose: 1. **^test Regex**: This is a custom implementation that matches strings starting with the literal text "test". It uses a regular expression (regex) with the caret `^` symbol, which anchors the match to the beginning of the string. 2. **.startsWith**: This is a built-in JavaScript method that checks if a string starts with another string. It returns `true` if the original string begins with the specified string, or `false` otherwise. 3. **.search(RegExp)**: This is a built-in JavaScript method that searches for a regex pattern within a string and returns the index of the match. If no match is found, it returns `-1`. 4. **.indexOf**: This is another built-in JavaScript method that finds the index of a specified value in a string. It's similar to `.search`, but returns the index of the first occurrence of the value instead of the matched pattern. **Pros and Cons** Here are some pros and cons for each approach: 1. **^test Regex**: * Pros: Customizable, exact match (starts with "test"). * Cons: May be slower due to regex processing. 2. **.startsWith**: * Pros: Built-in method, simple implementation. * Cons: May not work correctly for non-string values or edge cases. 3. **.search(RegExp)**: * Pros: Flexible regex patterns, efficient matching. * Cons: Requires explicit regex pattern creation and handling errors. 4. **.indexOf**: * Pros: Simple, fast indexing. * Cons: Returns index of first occurrence, not exact match. **Other Considerations** * The benchmark uses a custom implementation for `^test Regex`, which may affect the results. Standardized implementations might yield different performance characteristics. * The use of built-in methods like `.startsWith` and `.indexOf` can provide good performance, as they are optimized for these specific use cases. * The custom regex pattern in `^test Regex` is a simple string match, whereas `.search(RegExp)` allows more complex patterns. This difference may impact performance. **Benchmark Results** The latest benchmark results show the performance differences between each method. Chrome 95 on Mac OS X 10.15.7 executes: * `.indexOf`: ~295ms per second * `.startsWith`: ~257ms per second * `^test Regex`: ~319ms per second (custom implementation) * `.search(RegExp)`: ~257ms per second Keep in mind that these results may vary depending on the specific use case, input data, and environment.
Related benchmarks:
Regex vs .indexOf vs .startsWith
Regex vs .indexOf vs .startsWith 2
JS Regex vs .startsWith vs .indexOf Jan
Regex vs .indexOf vs .includes
JavaScript: Regex vs .startsWith vs .indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?