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










share|improve this question







New contributor




voidhammer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























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










    share|improve this question







    New contributor




    voidhammer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















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










      share|improve this question







      New contributor




      voidhammer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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







      New contributor




      voidhammer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      voidhammer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      voidhammer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 3 hours ago









      voidhammer

      61




      61




      New contributor




      voidhammer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      voidhammer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      voidhammer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          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.






          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
            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


















          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.






          share|improve this answer




























            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.






            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',
              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
              });


              }
              });






              voidhammer is a new contributor. Be nice, and check out our Code of Conduct.










              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








              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.






              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
                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















              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.






              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
                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













              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.






              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 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


















              • 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












              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.






              share|improve this answer

























                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.






                share|improve this answer























                  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.






                  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 3 hours ago









                  Jonathon Chambers

                  1789




                  1789






















                      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.






                      share|improve this answer

























                        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.






                        share|improve this answer























                          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.






                          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 3 hours ago









                          Jayant Das

                          11.1k2523




                          11.1k2523






















                              voidhammer is a new contributor. Be nice, and check out our Code of Conduct.










                              draft saved

                              draft discarded


















                              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.




                              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

                              What visual should I use to simply compare current year value vs last year in Power BI desktop

                              Alexandru Averescu

                              Trompette piccolo