I am unsure of which of these ways is faster to use multiple times, testing with a lot of string variables.
Which of these is faster to use for checking if the string is just whitespace?
if (str.trim().length > 0) {
}
Or
if (str.trim() !== '') {
}
Well, why not test it? http://jsperf.com/empty-string-comparison2
In terms of calculations per second, they differ by less than 1% (at least on Chromium). Unless you're testing millions of strings every second, I wouldn't worry about it.
The short answer is "benchmark and find out!". If you do this, you can also try using a regexp and see how fast that is:
if (str.match(/^\s*$/))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With