Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
split vs exec vs replace
(version: 0)
Test String.split vs RegEx.exec (and String.match) vs String.replace
Comparing performance of:
String.split vs String.match vs RegEx.exec vs String.replace
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
str = "1875 Tokens"; re = /(\d+)\sTokens/;
Tests:
String.split
str.split(" ")[0]
String.match
str.match(re)[1]
RegEx.exec
re.exec(str)[1]
String.replace
str.replace(' Tokens', '')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
String.split
String.match
RegEx.exec
String.replace
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 YaBrowser/25.2.0.0 Safari/537.36
Browser/OS:
Yandex Browser 25 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String.split
11222943.0 Ops/sec
String.match
7364552.0 Ops/sec
RegEx.exec
8170995.5 Ops/sec
String.replace
11633856.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare the performance of three different string manipulation techniques: 1. `String.split()` 2. `String.match()` (using regular expressions) 3. `String.replace()` **Options Compared** * `String.split()`: This method splits a string into an array of substrings based on a specified separator. * `String.match()`: This method returns an array containing the first match, if any, in the string using a regular expression. * `String.replace()`: This method replaces an occurrence of a pattern (in this case, a simple whitespace character) with another value. **Pros and Cons** * `String.split()`: Pros - efficient for separating strings by a fixed separator. Cons - may not be suitable for more complex separation logic, and it doesn't handle edge cases like null or undefined separators. * `String.match()`: Pros - flexible and powerful for pattern matching, but can be slow if the regular expression is complex. Cons - returns an array, which might require extra processing to extract the desired value. * `String.replace()`: Pros - simple and efficient, with a fixed replacement value. Cons - not suitable for complex replacement logic or multiple occurrences. **Library and Syntax** The benchmark uses JavaScript's built-in string manipulation methods, specifically: * No external libraries are used in this benchmark. * No special JavaScript features or syntax are required beyond what's provided by the standard library. **Benchmark Test Cases** Each test case measures the performance of one specific method. The individual test cases are: 1. `str.split(" ")`: Measures the time taken to split a string into an array using whitespace as the separator. 2. `str.match(re)`: Measures the time taken to extract the first match from the string using a regular expression. 3. `re.exec(str)`: Measures the time taken to execute a regular expression on the string and return a match object (in this case, only extracting the first group). 4. `str.replace(' Tokens', '')`: Measures the time taken to replace occurrences of whitespace with an empty string. **Other Alternatives** If you were to modify or extend this benchmark, here are some alternative approaches: * Use other regular expression flavors, like `.NET's Regex` or Perl's `regex`. * Compare performance using more complex separation logic (e.g., `String.localeCompare()`). * Measure the performance of other string manipulation methods, such as `String.prototype.replace()` with a callback function. * Compare the performance of different JavaScript engines (e.g., V8 vs SpiderMonkey) by running multiple benchmark iterations.
Related benchmarks:
str.match vs
split vs exec vs replace vs slice
Regex Exec vs String Split
bench simple split vs replace regex
Comments
Confirm delete:
Do you really want to delete benchmark?