Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex /i vs .indexOf with tolowerCase
(version: 0)
Testing some things
Comparing performance of:
Regex vs .indexOf
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regex = /test/i; 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.toLowerCase().indexOf(match.toLowerCase()) > -1; 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:
11 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex
686.1 Ops/sec
.indexOf
33289.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Overview** The provided JSON represents a benchmark that tests two approaches for searching strings: regular expressions (`/i`) and `.indexOf()`. The test creates an array of random strings, which are then used to measure the performance of each approach. The benchmark is designed to mimic real-world scenarios where you might need to search for patterns within strings. **Options Compared** Two options are compared: 1. **Regular Expressions (`/i`)**: This option uses a regular expression with the `i` flag, which makes the match case-insensitive. The regular expression is defined as `window.regex = /test/i;`. 2. **`.indexOf()`**: This option uses the `.indexOf()` method to search for a specific substring within each string. The test searches for the string "test" (case-sensitive) using `str.toLowerCase().indexOf(match.toLowerCase()) > -1;`. **Pros and Cons** Here's a brief overview of the pros and cons of each approach: **Regular Expressions (`/i`)** Pros: * Can be more flexible and powerful than `.indexOf()`, especially when dealing with complex patterns. * Can be faster for large datasets, as it uses a single operation to match the entire string. Cons: * Can be slower for simple searches, as it involves compiling the regular expression and executing the matching logic. * May have performance issues if the pattern is too complex or contains many groups. **.indexOf()** Pros: * Typically faster and more efficient than regular expressions for simple searches. * Easy to implement and understand, especially for simple patterns. Cons: * May not be suitable for complex patterns or large datasets. * Can be slower for strings with special characters or Unicode codes. **Library and Purpose** The `RegExp` object is used in the first option. It's a built-in JavaScript object that provides regular expression matching and searching capabilities. In the second option, `.indexOf()` is a method provided by the String object. It returns the index of the first occurrence of the specified substring within the string. **Special JS Feature** The benchmark uses the `window` object to create global variables, which is a common pattern in JavaScript benchmarks. This allows for easier setup and execution of the benchmark across different environments. The benchmark also uses a simple random number generator (`getRandomInt`) to generate random strings and numbers, which helps to simulate real-world scenarios and avoid biases. **Alternatives** Some alternative approaches to regular expressions or `.indexOf()` could include: * Using other string searching methods, such as `includes()`, `startsWith()`, or `endsWith()`. * Employing more advanced techniques, like suffix trees or trie data structures. * Utilizing specialized libraries or frameworks for text processing and search. Keep in mind that the choice of approach depends on the specific use case and performance requirements. The benchmark provides a good starting point for understanding the relative performance of different approaches.
Related benchmarks:
Regex vs .indexOf vs .startsWith 2
Regex vs .indexOf vs .includes
JavaScript Case Insensitive String Start: regex vs startsWith() vs indexOf() vs localeCompare()
Regex /i vs .indexOf with tolowerCase once
Comments
Confirm delete:
Do you really want to delete benchmark?