Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regex vs split and trim 2
(version: 0)
Comparing performance of:
Short regex vs Long regex vs Short split and trim vs Long split and trim
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
function generateText(length, containsOtherChars) { const str = [].constructor(length) .map(it => Math.random() > 0.5 ? ',' : ' ') .join(""); return containsOtherChars ? `${str}a` : str; } var shortEmptyText = generateText(100, false); var shortNonEmptyText = generateText(100, true); var longEmptyText = generateText(1000 * 1000 * 10, false); var longNonEmptyText = generateText(1000 * 1000 * 10, true); var regex = /^[\s,]*$/;
Tests:
Short regex
console.log(!!shortEmptyText.match(regex)?.length); console.log(!!shortNonEmptyText.match(regex)?.length);
Long regex
console.log(!!longEmptyText.match(regex)?.length); console.log(!!longNonEmptyText.match(regex)?.length);
Short split and trim
var sanitizedEmptyText = shortEmptyText.split(",").filter(it => !!it?.trim()).join(""); var sanitizedNonEmptyText = shortEmptyText.split(",").filter(it => !!it?.trim()).join(""); console.log(sanitizedEmptyText.length > 0); console.log(sanitizedNonEmptyText.length > 0);
Long split and trim
var sanitizedEmptyText = longEmptyText.split(",").filter(it => !!it?.trim()).join(""); var sanitizedNonEmptyText = longNonEmptyText.split(",").filter(it => !!it?.trim()).join(""); console.log(sanitizedEmptyText.length > 0); console.log(sanitizedNonEmptyText.length > 0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Short regex
Long regex
Short split and trim
Long split and trim
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/128.0.0.0 Safari/537.36 Edg/128.0.0.0
Browser/OS:
Chrome 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Short regex
185769.7 Ops/sec
Long regex
185769.7 Ops/sec
Short split and trim
174830.1 Ops/sec
Long split and trim
163241.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents a JavaScript benchmark test on the MeasureThat.net website. The test is designed to measure the performance of regular expression matching versus string splitting and trimming. **Benchmark Definition:** The benchmark definition consists of two parts: 1. **Script Preparation Code:** This section generates two types of strings, `shortEmptyText` and `longEmptyText`, which are used in the subsequent tests. The generated strings contain a mix of whitespace characters (spaces and commas) and may or may not contain the character 'a'. A regular expression, `/^[\\s,]*$/`, is defined to match any string that consists only of whitespace characters and commas. 2. **Html Preparation Code:** This section is empty, indicating that no HTML-specific preparation code is required for this benchmark. **Individual Test Cases:** There are four test cases: 1. **Short regex**: This test case uses the `shortEmptyText` and `longEmptyText` strings generated earlier to measure the performance of regular expression matching (`match()` method) on these strings. 2. **Long regex**: Similar to the previous test case, but using the `longEmptyText` string. 3. **Short split and trim**: This test case uses the same `shortEmptyText` string to measure the performance of splitting the string by commas (`split()`) and then trimming each resulting substring (`filter()` method). 4. **Long split and trim**: Similar to the previous test case, but using the `longEmptyText` string. **Options Compared:** The two main options being compared are: 1. Regular expression matching (`match()` method) 2. String splitting and trimming (`split()` and `filter()` methods) **Pros and Cons of Each Approach:** Regular Expression Matching (Regex): Pros: * Highly expressive and flexible for pattern matching * Can be used to validate input data Cons: * Typically slower than string manipulation methods due to the overhead of compiling and executing regex patterns * May not perform well with large or complex patterns String Splitting and Trimming: Pros: * Often faster than regular expression matching due to reduced overhead and cache-friendly operations * More straightforward for simple use cases, such as splitting a comma-separated list Cons: * Limited expressiveness compared to regular expressions * May require additional processing steps, such as trimming whitespace characters **Library Usage:** None of the test cases explicitly uses any external libraries. However, it's worth noting that JavaScript engines may use internal libraries or optimizations under the hood. **Special JS Features:** No special JavaScript features are explicitly used in this benchmark. **Alternatives:** If you're looking for alternatives to this benchmark, here are a few options: 1. **String manipulation benchmarks:** MeasureThat.net offers several string manipulation benchmarks, such as finding all occurrences of a substring or extracting specific fields from an array. 2. **Regular expression benchmarking tools:** Tools like RegexBench or RegExr provide more comprehensive and detailed regular expression benchmarking capabilities. 3. **JavaScript engine-specific benchmarks:** You can also explore benchmarking JavaScript engines directly, such as the Node.js V8 benchmark or the Chrome V8 benchmark. Keep in mind that these alternatives may not be directly comparable to this specific benchmark, but they can provide valuable insights into different aspects of JavaScript performance and optimization.
Related benchmarks:
double split vs regex
trim + length VS regex with prepared rx
String.prototype.split vs String.prototype.match
Regex vs split/join checking alphanumeric big number
str.match vs str.split vs deconstruction
Comments
Confirm delete:
Do you really want to delete benchmark?