Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs indexOf vs Set
(version: 1)
Testing some things
Comparing performance of:
Regex vs indexOf vs Set
Created:
3 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regex = /^test/; var match = window.match = 'test'; var data = window.data = []; const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var TOTAL_STRINGS = window.TOTAL_STRINGS = 100000; var set = new Set(); 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) { let _str = makeRandomString(getRandomInt(20)); data.push(_str); set.add(_str); }
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; }
Set
set.has(match);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Regex
indexOf
Set
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):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Overview** The benchmark compares three approaches to search for a specific pattern in a large dataset: 1. **Regular Expressions (Regex)** 2. **String `indexOf` method** 3. **Set data structure** **Benchmark Script Preparation Code** Before running the tests, the script preparation code sets up some global variables: * `window.regex`: a regular expression object (`/^test/`) * `window.match`: a string to be searched for (`'test'`) * `window.data`: an array of 100,000 random strings generated using `makeRandomString(len)`. * `window.set`: a Set data structure containing the same random strings as `window.data`. **Individual Test Cases** The benchmark consists of three test cases: 1. **Regex**: Searches for the regular expression (`/test/`) in each string of `window.data` and increments a counter until all 100,000 strings have been processed. 2. **indexOf**: Searches for the string `'test'` in each string of `window.data` using the `indexOf` method and increments a counter until all 100,000 strings have been processed. 3. **Set**: Checks if the Set `window.set` contains the string `'test'`. **Pros and Cons** * **Regex**: + Pros: flexible pattern matching, can be used for more complex searches. + Cons: slower performance compared to other methods, especially for large datasets. * **indexOf**: + Pros: fast performance, simple implementation. + Cons: only finds the first occurrence of the search string, may not work correctly if the string is very long or contains multiple occurrences. * **Set**: + Pros: fast membership testing, good cache locality. + Cons: requires additional memory to store the data structure, slower than `indexOf` for simple searches. **Library and Special JS Features** In this benchmark, the following libraries are used: * None explicitly, but the `Set` data structure is a built-in JavaScript API. There are no special JS features used in this benchmark. **Other Alternatives** Some alternative approaches to searching for patterns in strings could include: * Using a library like `String.prototype.includes()` or `Array.prototype.some()`, which may provide better performance than manual implementation. * Using a more advanced data structure, such as a trie or a suffix tree, for efficient pattern matching. * Implementing a custom binary search algorithm to find the first occurrence of the search string. Keep in mind that the choice of approach depends on the specific requirements and constraints of the problem you're trying to solve.
Related benchmarks:
Regex vs .indexOf vs .startsWith
Regex vs .indexOf vs .startsWith 2
Regex vs .indexOf vs .includes
Regex vs .indexOf vs .startsWith vs .indexof
Comments
Confirm delete:
Do you really want to delete benchmark?