var strs = [
"tag:cool",
"a8798d75-18ed-473b-a28b-0b756dd99151",
"asdf",
"othertag:",
"asdf:qwer:test",
":hmm",
":",
]
function doSplit(str) {
const parts = str.split(':')
if (parts.length !== 2) {
return null
}
return parts[0]
}
for (const str of strs) {
doSplit(str)
}
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)
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
split | |
rgx |
Test name | Executions per second |
---|---|
split | 5962339.5 Ops/sec |
rgx | 2591170.5 Ops/sec |
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:
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:
split
Test Case: This test case uses the split()
method to split each input string by colon (:
) and returns the first part.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.
rgx
Test Case: This test case uses a regular expression (rgx
) to match each input string and returns the captured group (tag
) if found.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:
split
: With an execution rate of 5962339.5 executions per second.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:
lodash.string
.However, these alternatives are not explicitly mentioned in the benchmark definition.