Which is Better/Faster isEmpty() or isBlank()?












2














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()?










share|improve this question



























    2














    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()?










    share|improve this question

























      2












      2








      2







      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()?










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 11 at 15:15









      voidhammer

      132




      132






















          3 Answers
          3






          active

          oldest

          votes


















          7














          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.






          share|improve this answer























          • 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
            Dec 11 at 15:35






          • 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
            Dec 11 at 15:38










          • 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
            Dec 11 at 16:13










          • @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
            Dec 11 at 16:29



















          2














          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.






          share|improve this answer





























            0














            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.






            share|improve this answer





















              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "459"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              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

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              7














              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.






              share|improve this answer























              • 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
                Dec 11 at 15:35






              • 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
                Dec 11 at 15:38










              • 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
                Dec 11 at 16:13










              • @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
                Dec 11 at 16:29
















              7














              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.






              share|improve this answer























              • 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
                Dec 11 at 15:35






              • 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
                Dec 11 at 15:38










              • 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
                Dec 11 at 16:13










              • @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
                Dec 11 at 16:29














              7












              7








              7






              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.






              share|improve this answer














              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.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Dec 11 at 16:33

























              answered Dec 11 at 15:30









              sfdcfox

              247k11188424




              247k11188424












              • 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
                Dec 11 at 15:35






              • 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
                Dec 11 at 15:38










              • 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
                Dec 11 at 16:13










              • @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
                Dec 11 at 16:29


















              • 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
                Dec 11 at 15:35






              • 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
                Dec 11 at 15:38










              • 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
                Dec 11 at 16:13










              • @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
                Dec 11 at 16:29
















              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
              Dec 11 at 15:35




              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
              Dec 11 at 15:35




              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
              Dec 11 at 15:38




              @JayantDas Yeah, a difference this small means about 0.00012ms per use, so you would need millions of uses to make a difference.
              – sfdcfox
              Dec 11 at 15:38












              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
              Dec 11 at 16:13




              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
              Dec 11 at 16:13












              @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
              Dec 11 at 16:29




              @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
              Dec 11 at 16:29













              2














              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.






              share|improve this answer


























                2














                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.






                share|improve this answer
























                  2












                  2








                  2






                  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.






                  share|improve this answer












                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 11 at 15:34









                  Jayant Das

                  12.1k2623




                  12.1k2623























                      0














                      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.






                      share|improve this answer


























                        0














                        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.






                        share|improve this answer
























                          0












                          0








                          0






                          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.






                          share|improve this answer












                          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.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 11 at 15:27









                          Jonathon Chambers

                          1889




                          1889






























                              draft saved

                              draft discarded




















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              Popular posts from this blog

                              "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                              Alcedinidae

                              Origin of the phrase “under your belt”?