Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs split/join vs substring
(version: 0)
Comparing performance of:
Regex vs Split and Join vs indexof
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = 'hEllo#hello#####jhSdjf#';
Tests:
Regex
const [whole, one, two] = [...str.matchAll(/([^#]*)#(.*)/gmi)]; const result = one+"#"+two
Split and Join
const [beforeHash, ...afterFirstHash] = str.split('#'); const result = beforeHash+"#"+afterFirstHash.join("#")
indexof
const ind = str.indexOf('#'); const result = str.substring(0, ind) + "#" + str.substring(ind+1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Regex
Split and Join
indexof
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 microbenchmarks! **Benchmark Overview** The benchmark measures the performance of three different approaches to process a string: using regular expressions (`Regex`), splitting and joining the string (`Split and Join`), and finding the index of a substring (`indexof`). The test cases use a provided input string `str`, which contains multiple occurrences of the same pattern, followed by different delimiters (#). **Options Compared** The three options are compared in terms of their execution speed: 1. **Regex**: Uses regular expressions to match patterns and extract substrings. 2. **Split and Join**: Splits the string into substrings using a delimiter (`#`) and then joins them back together. 3. **indexof**: Finds the index of the first occurrence of a substring within the original string. **Pros and Cons** 1. **Regex**: * Pros: Can be flexible, efficient for complex patterns, and supports advanced features like capturing groups and lookaheads. * Cons: Can be slow due to the overhead of compiling regular expressions and iterating over the input data. 2. **Split and Join**: * Pros: Can be simple to implement, efficient for simple cases with a fixed delimiter, and can take advantage of optimized string splitting and joining algorithms. * Cons: May not perform well if the input string contains multiple occurrences of the same pattern, leading to unnecessary splits and joins. Also, it's limited to using a specific delimiter (#). 3. **indexof**: * Pros: Can be fast, as it only requires iterating over the input data once. * Cons: May not perform well if the substring is not found in the input string, leading to an unnecessary search. **Library and Special JS Features** In this benchmark, no libraries are explicitly mentioned. However, regular expressions (`Regex`) use a built-in JavaScript API that provides a powerful way to match patterns in strings. The `matchAll` method used in one of the test cases is a newer feature introduced in ECMAScript 2019 (ES2020), which allows matching patterns in an array-like object. This makes it easier to extract multiple captures from a single regular expression. **Benchmark Preparation Code** The script preparation code provides the input string `str` and initializes variables for each test case: ```javascript var str = 'hEllo#hello#####jhSdjf#'; const [whole, one, two] = [...str.matchAll(/([^#]*)#(.*)/gmi)]; ``` This code uses the regular expression `/([^#]*)#(.*)/` to match patterns in `str`. The `matchAll` method returns an array of matches, where each match is an array containing the captured groups (`whole`, `one`, and `two`).
Related benchmarks:
Regex vs split/join on simple case
Simple Regex vs split/join
Regex vs split/join 2
Regex vs split/join checking
Regex vs split/join 23313
Comments
Confirm delete:
Do you really want to delete benchmark?