Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RW - test
(version: 0)
Testing some things
Comparing performance of:
Regex vs .indexOf
Created:
2 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))); } console.log(data);
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; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Regex
.indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex
633.5 Ops/sec
.indexOf
12354.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided JSON and explain what's being tested, compared, and other considerations. **Benchmark Definition** The benchmark definition is a JavaScript code snippet that creates a test scenario. It defines: * `TOTAL_STRINGS`: an integer variable set to 100,000 * `data`: an array of strings generated using the `makeRandomString` function (explained later) * `regex`: a regular expression object assigned to the global scope The benchmark prepares data by populating the `data` array with random strings. It then uses two functions: 1. `getRandomInt(max)`: generates a random integer between 0 and `max` 2. `makeRandomString(len)`: generates a random string of length `len` using a predefined character set (`possible`) The benchmark loops through the `data` array, applying one of two operations to each string: 1. **Regex**: uses the `regex.test(str)` method to test if the string matches the regular expression 2. **.indexOf**: uses the `.indexOf(match)` method to check if the first character of the string is equal to the value of the global variable `match` **Options being compared** The benchmark compares two different approaches: 1. **Regex**: uses a regular expression object to test strings for pattern matching 2. **.indexOf**: uses the `.indexOf()` method to search for a specific substring in a string **Pros and Cons** Here's a brief overview of the pros and cons of each approach: * **Regex**: + Pros: - Flexible and powerful pattern-matching capabilities - Can be used to validate input data, extract patterns from text, and more + Cons: - Performance can degrade with complex patterns or large inputs - May have issues with Unicode characters or edge cases * **.indexOf**: + Pros: - Fast and efficient for searching a specific substring in a string - Can be used to perform simple substring matching + Cons: - Limited functionality compared to regular expressions - May not work correctly with Unicode characters or edge cases **Library** There is no explicit library mentioned in the benchmark definition, but it uses the following built-in JavaScript features: * `Math.floor()` * `Math.random()` No external libraries are required for this benchmark. **Special JS feature/syntax** The benchmark utilizes a special syntax for global variable assignment using `window.xxx = value;`. This is used to assign values to global variables in the context of the test. **Other considerations** The benchmark uses a simple and straightforward approach, which makes it easy to understand and compare. However, it's worth noting that this benchmark may not accurately represent real-world use cases, as it only tests a specific scenario with predefined data. More comprehensive benchmarks might involve larger datasets or more complex use cases. If you're looking for alternatives, here are some options: * **Benchmarking frameworks**: There are many benchmarking frameworks available, such as `Benchmark.js` or `benchmark-clone`, that can help you create and run benchmark tests. * **JavaScript performance testing tools**: Tools like `Google Closure Compiler` or `ES6 performance optimization` can help you optimize JavaScript code for better performance. * **Regular expression testing libraries**: Libraries like `regex-test` or ` regex-pattern-match` can provide additional functionality for testing regular expressions.
Related benchmarks:
indexOf vs reg
indexOf vs startsWith
.substr vs .startwith
.startsWith vs .charAt vs str[0] for single character
Comments
Confirm delete:
Do you really want to delete benchmark?