Which is Better/Faster isEmpty() or isBlank()?
up vote
1
down vote
favorite
There is method, that have some condition flow and returning empty String ''(no space) in some case. Which method is more effective/faster to use in this case: String.isEmpty() or String.isBlack()?
apex string
New contributor
add a comment |
up vote
1
down vote
favorite
There is method, that have some condition flow and returning empty String ''(no space) in some case. Which method is more effective/faster to use in this case: String.isEmpty() or String.isBlack()?
apex string
New contributor
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
There is method, that have some condition flow and returning empty String ''(no space) in some case. Which method is more effective/faster to use in this case: String.isEmpty() or String.isBlack()?
apex string
New contributor
There is method, that have some condition flow and returning empty String ''(no space) in some case. Which method is more effective/faster to use in this case: String.isEmpty() or String.isBlack()?
apex string
apex string
New contributor
New contributor
New contributor
asked 3 hours ago
voidhammer
61
61
New contributor
New contributor
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
6
down vote
String.isEmpty
is marginally faster (~0.00219ms vs ~0.00206ms, about 6%). This is such a trivially small amount that there's no reason to worry about which one you use from a performance perspective.
Practically speaking, you should generally use String.isBlank
if you expect potentially whitespace strings (e.g. from user input that may be all whitespace), and String.isEmpty
when checking fields from the database, which will never contain a string of just whitespace.
I haven't really tested it out, but would agree with such marginal difference, it mostly depends on what you want to validate -- blank vs. empty.
– Jayant Das
3 hours ago
1
@JayantDas Yeah, a difference this small means about 0.00012ms per use, so you would need millions of uses to make a difference.
– sfdcfox
3 hours ago
Haven't you mixed these two up?isBlank
is the one when you expect whitespace strings and not the other way around, and it must be the slower one since it needs to check characters and not just length.
– Jake Cobb
2 hours ago
@JakeCobb That's what I get for answering before coffee. I just realized i mixed up my variables in the benchmark I was using. One sec...
– sfdcfox
2 hours ago
add a comment |
up vote
0
down vote
String.isEmpty()
will be better. Assuming that you are not expecting strings containing just whitespace returning a true
value. If you want it to also treat strings containing whitespace as empty use String.isBlank()
The difference between the two is very slight but the docs specify what will be returned depending on input.
add a comment |
up vote
0
down vote
Faster here is very broad to classify, as you will need lot of considerations to test that out. It depends on what is your use case and accordingly which method fits in best.
String.isBlank
handles any string even with white space, whereas the same is not true for String.isEmpty
. So if you expect empty string (''
) always, then using either of them should be fine.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
String.isEmpty
is marginally faster (~0.00219ms vs ~0.00206ms, about 6%). This is such a trivially small amount that there's no reason to worry about which one you use from a performance perspective.
Practically speaking, you should generally use String.isBlank
if you expect potentially whitespace strings (e.g. from user input that may be all whitespace), and String.isEmpty
when checking fields from the database, which will never contain a string of just whitespace.
I haven't really tested it out, but would agree with such marginal difference, it mostly depends on what you want to validate -- blank vs. empty.
– Jayant Das
3 hours ago
1
@JayantDas Yeah, a difference this small means about 0.00012ms per use, so you would need millions of uses to make a difference.
– sfdcfox
3 hours ago
Haven't you mixed these two up?isBlank
is the one when you expect whitespace strings and not the other way around, and it must be the slower one since it needs to check characters and not just length.
– Jake Cobb
2 hours ago
@JakeCobb That's what I get for answering before coffee. I just realized i mixed up my variables in the benchmark I was using. One sec...
– sfdcfox
2 hours ago
add a comment |
up vote
6
down vote
String.isEmpty
is marginally faster (~0.00219ms vs ~0.00206ms, about 6%). This is such a trivially small amount that there's no reason to worry about which one you use from a performance perspective.
Practically speaking, you should generally use String.isBlank
if you expect potentially whitespace strings (e.g. from user input that may be all whitespace), and String.isEmpty
when checking fields from the database, which will never contain a string of just whitespace.
I haven't really tested it out, but would agree with such marginal difference, it mostly depends on what you want to validate -- blank vs. empty.
– Jayant Das
3 hours ago
1
@JayantDas Yeah, a difference this small means about 0.00012ms per use, so you would need millions of uses to make a difference.
– sfdcfox
3 hours ago
Haven't you mixed these two up?isBlank
is the one when you expect whitespace strings and not the other way around, and it must be the slower one since it needs to check characters and not just length.
– Jake Cobb
2 hours ago
@JakeCobb That's what I get for answering before coffee. I just realized i mixed up my variables in the benchmark I was using. One sec...
– sfdcfox
2 hours ago
add a comment |
up vote
6
down vote
up vote
6
down vote
String.isEmpty
is marginally faster (~0.00219ms vs ~0.00206ms, about 6%). This is such a trivially small amount that there's no reason to worry about which one you use from a performance perspective.
Practically speaking, you should generally use String.isBlank
if you expect potentially whitespace strings (e.g. from user input that may be all whitespace), and String.isEmpty
when checking fields from the database, which will never contain a string of just whitespace.
String.isEmpty
is marginally faster (~0.00219ms vs ~0.00206ms, about 6%). This is such a trivially small amount that there's no reason to worry about which one you use from a performance perspective.
Practically speaking, you should generally use String.isBlank
if you expect potentially whitespace strings (e.g. from user input that may be all whitespace), and String.isEmpty
when checking fields from the database, which will never contain a string of just whitespace.
edited 2 hours ago
answered 3 hours ago
sfdcfox
243k10185414
243k10185414
I haven't really tested it out, but would agree with such marginal difference, it mostly depends on what you want to validate -- blank vs. empty.
– Jayant Das
3 hours ago
1
@JayantDas Yeah, a difference this small means about 0.00012ms per use, so you would need millions of uses to make a difference.
– sfdcfox
3 hours ago
Haven't you mixed these two up?isBlank
is the one when you expect whitespace strings and not the other way around, and it must be the slower one since it needs to check characters and not just length.
– Jake Cobb
2 hours ago
@JakeCobb That's what I get for answering before coffee. I just realized i mixed up my variables in the benchmark I was using. One sec...
– sfdcfox
2 hours ago
add a comment |
I haven't really tested it out, but would agree with such marginal difference, it mostly depends on what you want to validate -- blank vs. empty.
– Jayant Das
3 hours ago
1
@JayantDas Yeah, a difference this small means about 0.00012ms per use, so you would need millions of uses to make a difference.
– sfdcfox
3 hours ago
Haven't you mixed these two up?isBlank
is the one when you expect whitespace strings and not the other way around, and it must be the slower one since it needs to check characters and not just length.
– Jake Cobb
2 hours ago
@JakeCobb That's what I get for answering before coffee. I just realized i mixed up my variables in the benchmark I was using. One sec...
– sfdcfox
2 hours ago
I haven't really tested it out, but would agree with such marginal difference, it mostly depends on what you want to validate -- blank vs. empty.
– Jayant Das
3 hours ago
I haven't really tested it out, but would agree with such marginal difference, it mostly depends on what you want to validate -- blank vs. empty.
– Jayant Das
3 hours ago
1
1
@JayantDas Yeah, a difference this small means about 0.00012ms per use, so you would need millions of uses to make a difference.
– sfdcfox
3 hours ago
@JayantDas Yeah, a difference this small means about 0.00012ms per use, so you would need millions of uses to make a difference.
– sfdcfox
3 hours ago
Haven't you mixed these two up?
isBlank
is the one when you expect whitespace strings and not the other way around, and it must be the slower one since it needs to check characters and not just length.– Jake Cobb
2 hours ago
Haven't you mixed these two up?
isBlank
is the one when you expect whitespace strings and not the other way around, and it must be the slower one since it needs to check characters and not just length.– Jake Cobb
2 hours ago
@JakeCobb That's what I get for answering before coffee. I just realized i mixed up my variables in the benchmark I was using. One sec...
– sfdcfox
2 hours ago
@JakeCobb That's what I get for answering before coffee. I just realized i mixed up my variables in the benchmark I was using. One sec...
– sfdcfox
2 hours ago
add a comment |
up vote
0
down vote
String.isEmpty()
will be better. Assuming that you are not expecting strings containing just whitespace returning a true
value. If you want it to also treat strings containing whitespace as empty use String.isBlank()
The difference between the two is very slight but the docs specify what will be returned depending on input.
add a comment |
up vote
0
down vote
String.isEmpty()
will be better. Assuming that you are not expecting strings containing just whitespace returning a true
value. If you want it to also treat strings containing whitespace as empty use String.isBlank()
The difference between the two is very slight but the docs specify what will be returned depending on input.
add a comment |
up vote
0
down vote
up vote
0
down vote
String.isEmpty()
will be better. Assuming that you are not expecting strings containing just whitespace returning a true
value. If you want it to also treat strings containing whitespace as empty use String.isBlank()
The difference between the two is very slight but the docs specify what will be returned depending on input.
String.isEmpty()
will be better. Assuming that you are not expecting strings containing just whitespace returning a true
value. If you want it to also treat strings containing whitespace as empty use String.isBlank()
The difference between the two is very slight but the docs specify what will be returned depending on input.
answered 3 hours ago
Jonathon Chambers
1789
1789
add a comment |
add a comment |
up vote
0
down vote
Faster here is very broad to classify, as you will need lot of considerations to test that out. It depends on what is your use case and accordingly which method fits in best.
String.isBlank
handles any string even with white space, whereas the same is not true for String.isEmpty
. So if you expect empty string (''
) always, then using either of them should be fine.
add a comment |
up vote
0
down vote
Faster here is very broad to classify, as you will need lot of considerations to test that out. It depends on what is your use case and accordingly which method fits in best.
String.isBlank
handles any string even with white space, whereas the same is not true for String.isEmpty
. So if you expect empty string (''
) always, then using either of them should be fine.
add a comment |
up vote
0
down vote
up vote
0
down vote
Faster here is very broad to classify, as you will need lot of considerations to test that out. It depends on what is your use case and accordingly which method fits in best.
String.isBlank
handles any string even with white space, whereas the same is not true for String.isEmpty
. So if you expect empty string (''
) always, then using either of them should be fine.
Faster here is very broad to classify, as you will need lot of considerations to test that out. It depends on what is your use case and accordingly which method fits in best.
String.isBlank
handles any string even with white space, whereas the same is not true for String.isEmpty
. So if you expect empty string (''
) always, then using either of them should be fine.
answered 3 hours ago
Jayant Das
11.1k2523
11.1k2523
add a comment |
add a comment |
voidhammer is a new contributor. Be nice, and check out our Code of Conduct.
voidhammer is a new contributor. Be nice, and check out our Code of Conduct.
voidhammer is a new contributor. Be nice, and check out our Code of Conduct.
voidhammer is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f242159%2fwhich-is-better-faster-isempty-or-isblank%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown