Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
match vs test vs split vs trim to check for empty string or all spaces
(version: 0)
Comparing performance of:
match vs test vs split vs trim
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.strs = [[' ', true], ['', true], ['asdf', false], [' ', true]]
Tests:
match
window.strs.forEach(([str, expected]) => { (str.match(/^ *$/) !== null) === expected; });
test
window.strs.forEach(([str, expected]) => { /^ *$/.test(str) === expected; });
split
window.strs.forEach(([str, expected]) => { (str.split(/^ *$/).length === 2) === expected; });
trim
window.strs.forEach(([str, expected]) => { (str.trim().length === 0) === expected; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
match
test
split
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/131.0.0.0 Safari/537.36 Edg/131.0.0.0
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
match
1487905.2 Ops/sec
test
1631909.6 Ops/sec
split
1415003.8 Ops/sec
trim
2661871.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net. The benchmark tests the performance of four different approaches to check if a string is empty or consists only of spaces: `match`, `test`, `split`, and `trim`. Here's an explanation of each approach, their pros and cons, and other considerations: 1. **match**: The `match` function uses a regular expression to check if the string starts with zero or more whitespace characters (`^ *`). This approach is concise and easy to read. Pros: Simple and efficient. Cons: May not be as readable as other approaches for those unfamiliar with regular expressions. 2. **test**: The `test` function uses a test string pattern (`/^ *$/`) to check if the input string matches it. This approach is more explicit and easier to understand than regular expressions. Pros: More readable than regular expressions, especially for those new to JavaScript. Cons: May be slower due to the overhead of using a test string pattern. 3. **split**: The `split` function splits the input string into an array of substrings using whitespace as the separator (`/^ *$/`). The length of the resulting array is then compared to 2 (since we expect only one empty substring). This approach is clever but may be less efficient than other methods. Pros: Concise and easy to understand. Cons: May have performance implications due to the overhead of splitting a string. 4. **trim**: The `trim` function removes leading and trailing whitespace from the input string, and then checks if its length is 0 (indicating only spaces). This approach is efficient but may be less readable than other methods. Pros: Efficient and easy to understand. Cons: May not be as concise as other approaches. All these approaches are valid solutions, and their performance differences will likely depend on the specific use case and JavaScript engine being used. The `trim` method seems to have the best balance between performance and readability in this benchmark. As for libraries or special features, none of these approaches rely on any external libraries or advanced JavaScript features other than basic string manipulation methods (like `split`, `trim`, and `match`).
Related benchmarks:
Regex First Name split vs match - no console
str.match vs str.Split1
Regex whitespace check vs trim length
Detecting an Empty or Whitespace String using RegEx vs trim
Comments
Confirm delete:
Do you really want to delete benchmark?