var testString = "test|https://www.cnn.com/2025/04/12/tech/trump-electronics-china-tariffs/index.html?iid=cnn_buildContentRecirc_end_recirc#openweb-convo|0-1,5-6,99-10101"
var values = testString.split("|");
var value1 = values[0];
var value2 = values[1];
var value3 = values[2];
var regex = '(.*?)\|(.*?)\|(.*)'
var value1 = regex[0];
var value2 = regex[1];
var value3 = regex[2];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Splitting | |
Regex |
Test name | Executions per second |
---|---|
Splitting | 39404960.0 Ops/sec |
Regex | 158853504.0 Ops/sec |
The benchmark you provided is titled "Split vs Regex but with URLs" and it tests the performance of two different methods for processing a string: splitting the string by a delimiter and using a regular expression to extract parts of the string.
Splitting:
var values = testString.split("|");
var value1 = values[0];
var value2 = values[1];
var value3 = values[2];
split()
function, which takes a delimiter (in this case, the pipe character "|") and divides the string into an array of substrings.split()
function is straightforward and easy to understand.Regex:
var regex = '(.*?)\\|(.*?)\\|(.*)';
var value1 = regex[0];
var value2 = regex[1];
var value3 = regex[2];
The benchmark results indicate that the Regex
approach achieved 746,145,856 executions per second, while the Splitting
method recorded 4,993,935 executions per second. This data shows that the regex method significantly outperformed the split method in this particular experiment.
split()
can enhance code clarity.String.prototype.slice()
for extraction without splitting, or other functional programming patterns depending on the use case.In summary, this benchmark compares two different approaches to string manipulation—simpler direct splitting versus more complex regex pattern matching. The choice between them will largely depend on specific use cases and the need for performance or clarity in your application.