Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex whitespace check vs trim length
(version: 0)
Comparing performance of:
regex vs trim
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = ' fsajdf asdfjosa fjoiawejf oawjfoei jaosdjfsdjfo sfjos 2324234 sdf safjao j o sdlfks dflks l '
Tests:
regex
!/\S/.test(str)
trim
!!str.trim().length
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
regex
trim
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 143 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
regex
31050934.0 Ops/sec
trim
37019048.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested on MeasureThat.net. **What is being tested?** The benchmark compares two approaches to check if a string contains whitespace: 1. **Regex**: Using a regular expression (`!/\S/.test(str)`) to check if the string has any non-whitespace characters. 2. **Trim length**: Checking the length of the trimmed string (`!!str.trim().length`) after removing all whitespace characters. **Options compared** The two options are compared in terms of performance, specifically: * How many times can each option execute per second on a specific browser and device platform? * Which approach is faster? **Pros and Cons of each approach:** 1. **Regex ( !/\S/.test(str) )** * Pros: + Simple to implement + Can be used for more complex pattern matching * Cons: + May be slower due to the overhead of regular expression execution + Can be less efficient in terms of CPU cycles compared to trimming and checking length directly 2. **Trim length ( !!str.trim().length )** * Pros: + Generally faster than regex due to fewer operations required + More straightforward and efficient approach for simple whitespace checks * Cons: + May require additional allocations for creating a new trimmed string object **Library used** None, but it's worth noting that the `trim()` method is part of the JavaScript String prototype. **Special JS feature or syntax** The question mark `!!` before `str.trim().length` is an example of optional chaining and the nullish coalescing operator (`??`). This syntax allows for more concise expressions and can improve performance by avoiding unnecessary operations. However, it's not a fundamental feature that would be considered essential to understand for general JavaScript programming. **Other alternatives** If you were to write this benchmark yourself, you might also consider comparing other approaches, such as: * Using a dedicated whitespace removal library or function * Checking the length of the string before and after trimming to ensure accuracy * Comparing performance with different string types (e.g., text, buffer, etc.) * Incorporating more test cases to cover edge cases and variations in browser behavior
Related benchmarks:
Regex detecting whitespace vs trim
Regex detecting whitespace vs trim
Regex removing single whitespace vs multiple whitespaces
trim + length VS regex
Comments
Confirm delete:
Do you really want to delete benchmark?