Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs .indexOf vs .substr3
(version: 0)
Testing some things
Comparing performance of:
Regex vs .indexOf vs .substr
Created:
6 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; }
.substr
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.substring(0,6) === 'test12'; x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Regex
.indexOf
.substr
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 dive into the world of JavaScript benchmarks. **Benchmark Overview** MeasureThat.net is a platform that allows users to create and run JavaScript microbenchmarks. The benchmark in question compares three different approaches for searching for a specific string within an array of strings: regular expressions (`Regex`), `indexOf`, and `.substr`. **What's being tested?** The test consists of generating a large dataset of random strings (up to 20 characters long) and then testing each of the three approaches on this data. The approach is considered "fastest" if it can execute the most iterations per second. **Options compared:** 1. **Regex**: Uses a regular expression to search for the target string. 2. **.indexOf**: Uses the `indexOf` method to find the index of the first occurrence of the target string within the array. 3. **.substr**: Uses the `substring` method with slicing to extract a substring from the original string. **Pros and Cons:** 1. **Regex**: * Pros: Highly flexible, can match complex patterns, and is often used in other contexts. * Cons: Can be slow for simple searches due to the overhead of compiling regular expressions. 2. **.indexOf**: * Pros: Fast and efficient for simple string comparisons. * Cons: Only finds the first occurrence of the target string, may not work correctly if the array is modified during the search. 3. **.substr**: * Pros: Can be optimized for performance by using a caching mechanism to store previously computed substrings. * Cons: Requires more memory and computation compared to `indexOf`, as it needs to create a new substring object. **Library usage** The test uses the `window.regex` property, which is not a standard JavaScript library. Instead, it's likely a custom implementation of regular expressions for performance optimization. Similarly, `window.match` and `window.data` are custom variables used to store the target string and dataset, respectively. **Special JS feature/syntax** The test uses some advanced JavaScript features: * **Closures**: The `getRandomInt` function is a closure that captures its outer scope's variables. * **Arrow functions**: The `makeRandomString` function uses an arrow function for brevity and performance optimization. * **ES6 features**: The benchmark code uses modern JavaScript syntax, including template literals (`'test12'`) and the spread operator (`[...]`). **Alternatives** If you're interested in exploring other approaches or optimizations, here are some alternatives: 1. **Naive string comparison**: Iterate through each character of the input string to compare with the target substring. 2. ** Boyer-Moore algorithm**: A more efficient string searching algorithm that uses precomputations to skip over parts of the input string. 3. **Tries**: A data structure optimized for fast string matching, which can be used as an alternative to regular expressions. Keep in mind that each approach has its trade-offs and may not be suitable for all use cases or performance requirements.
Related benchmarks:
Regex vs .indexOf vs .startsWith
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?