Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regex v split parsez
(version: 0)
Comparing performance of:
split vs rgx
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var strs = [ "tag:cool", "a8798d75-18ed-473b-a28b-0b756dd99151", "asdf", "othertag:", "asdf:qwer:test", ":hmm", ":", ]
Tests:
split
function doSplit(str) { const parts = str.split(':') if (parts.length !== 2) { return null } return parts[0] } for (const str of strs) { doSplit(str) }
rgx
const rgx = /(?<tag>[a-z]+):(.*)?/ function doRgx(str) { const res = str.match(rgx) if (!res) { return null } return res.groups?.tag ?? null } for (const str of strs) { doRgx(str) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
split
rgx
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
split
5962339.5 Ops/sec
rgx
2591170.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided benchmark is designed to compare the performance of two different approaches for parsing and extracting data from strings: splitting by colon (`:`) using the `split()` method, and regular expression matching (`rgx`) with the `/` notation. The benchmark uses a dataset of 9 input strings containing tags and other characters. **Script Preparation Code** The script preparation code defines an array of strings (`strs`) that will be used as input for both test cases: ```javascript var strs = [ "tag:cool", "a8798d75-18ed-473b-a28b-0b756dd99151", "asdf", "othertag:", "asdf:qwer:test", ":hmm", ":", ]; ``` This script is executed before running the test cases. **Individual Test Cases** The benchmark consists of two individual test cases: 1. **`split` Test Case**: This test case uses the `split()` method to split each input string by colon (`:`) and returns the first part. ```javascript function doSplit(str) { const parts = str.split(':'); if (parts.length !== 2) { return null; } return parts[0]; } for (const str of strs) { doSplit(str); } ``` This test case is executed for each input string in the `strs` array. 2. **`rgx` Test Case**: This test case uses a regular expression (`rgx`) to match each input string and returns the captured group (`tag`) if found. ```javascript const rgx = /(?<tag>[a-z]+):(.*)?/; function doRgx(str) { const res = str.match(rgx); if (!res) { return null; } return res.groups?.tag ?? null; } for (const str of strs) { doRgx(str); } ``` This test case is executed for each input string in the `strs` array. **Library Used: None** There are no external libraries used in these test cases. The regular expression is defined inline using the `/` notation. **Special JS Features/Syntax: Regular Expressions (RegEx)** The test case using regular expressions (`rgx`) employs a feature called "captured groups" (indicated by `(?<tag>[a-z]+)`) to extract the tag from each input string. This allows for more complex pattern matching and is particularly useful when dealing with strings that contain multiple pieces of data. **Benchmark Result** The latest benchmark result shows two test cases: 1. **`split`**: With an execution rate of 5962339.5 executions per second. 2. **`rgx`**: With an execution rate of 2591170.5 executions per second. This suggests that the `split()` method is significantly faster than the regular expression approach for this specific use case. **Other Alternatives** Some alternative approaches to splitting strings by colon or using regular expressions could include: * Using a more specialized string manipulation library, such as `lodash.string`. * Implementing custom string parsing logic using a programming language like C++. * Utilizing a different data structure, such as a trie or suffix tree, to optimize the search and extraction process. However, these alternatives are not explicitly mentioned in the benchmark definition.
Related benchmarks:
String split vs Regex 02
Regex First Name split vs match - no console
regex vs split lucas ribeiro
occurences is string
String split separator vs regex
Comments
Confirm delete:
Do you really want to delete benchmark?