Does console.log only execute when you use inspect on website?









up vote
0
down vote

favorite












I have a lot of console.log in my script, and I don't want them slowing my script down when I don't have the console open.



So my question is, do they execute when the console is closed? Because if they do I would have to comment them out, and then uncomment them every time I need to see them.










share|improve this question





















  • Different browsers are different, but Firefox and Chrome definitely obey console.log() calls when the console is not visible.
    – Pointy
    Nov 12 at 3:28










  • @Pointy That unfortunate. So I would have to comment them out when I'm not using them to make my script run faster?
    – frosty
    Nov 12 at 3:30














up vote
0
down vote

favorite












I have a lot of console.log in my script, and I don't want them slowing my script down when I don't have the console open.



So my question is, do they execute when the console is closed? Because if they do I would have to comment them out, and then uncomment them every time I need to see them.










share|improve this question





















  • Different browsers are different, but Firefox and Chrome definitely obey console.log() calls when the console is not visible.
    – Pointy
    Nov 12 at 3:28










  • @Pointy That unfortunate. So I would have to comment them out when I'm not using them to make my script run faster?
    – frosty
    Nov 12 at 3:30












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a lot of console.log in my script, and I don't want them slowing my script down when I don't have the console open.



So my question is, do they execute when the console is closed? Because if they do I would have to comment them out, and then uncomment them every time I need to see them.










share|improve this question













I have a lot of console.log in my script, and I don't want them slowing my script down when I don't have the console open.



So my question is, do they execute when the console is closed? Because if they do I would have to comment them out, and then uncomment them every time I need to see them.







javascript console console.log userscripts






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 3:27









frosty

98411637




98411637











  • Different browsers are different, but Firefox and Chrome definitely obey console.log() calls when the console is not visible.
    – Pointy
    Nov 12 at 3:28










  • @Pointy That unfortunate. So I would have to comment them out when I'm not using them to make my script run faster?
    – frosty
    Nov 12 at 3:30
















  • Different browsers are different, but Firefox and Chrome definitely obey console.log() calls when the console is not visible.
    – Pointy
    Nov 12 at 3:28










  • @Pointy That unfortunate. So I would have to comment them out when I'm not using them to make my script run faster?
    – frosty
    Nov 12 at 3:30















Different browsers are different, but Firefox and Chrome definitely obey console.log() calls when the console is not visible.
– Pointy
Nov 12 at 3:28




Different browsers are different, but Firefox and Chrome definitely obey console.log() calls when the console is not visible.
– Pointy
Nov 12 at 3:28












@Pointy That unfortunate. So I would have to comment them out when I'm not using them to make my script run faster?
– frosty
Nov 12 at 3:30




@Pointy That unfortunate. So I would have to comment them out when I'm not using them to make my script run faster?
– frosty
Nov 12 at 3:30












2 Answers
2






active

oldest

votes

















up vote
0
down vote













Yes, console.log() in your code will execute without the console open on the web page.






share|improve this answer



























    up vote
    0
    down vote













    In all the browsers I know of, they do execute even while the console is closed - for example, try running the following code with the console closed, then open the console after 5 seconds, and you'll see that the console has indeed been populated with the text:






    setInterval(() => 
    console.log('log');
    , 1000);





    The performance impact of console.log is next to nothing, but if you wanted an easy way to toggle between having console.log calls print to the console, and do nothing, you can define a console variable on the top level of your userscript that has a log function which does nothing, thus shadowing window.console.log inside the scope of your userscript:






    const console = log: () => void 0 ;

    console.log('foo');





    To print to the console normally again, just comment out the const console... line (or use a boolean variable doPrintToConsole or something).



    This will not prevent native page scripts from console.logging. (If you wanted to prevent native page scripts from doing so, overwrite window.console.log instead);






    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      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: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      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%2fstackoverflow.com%2fquestions%2f53255588%2fdoes-console-log-only-execute-when-you-use-inspect-on-website%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      Yes, console.log() in your code will execute without the console open on the web page.






      share|improve this answer
























        up vote
        0
        down vote













        Yes, console.log() in your code will execute without the console open on the web page.






        share|improve this answer






















          up vote
          0
          down vote










          up vote
          0
          down vote









          Yes, console.log() in your code will execute without the console open on the web page.






          share|improve this answer












          Yes, console.log() in your code will execute without the console open on the web page.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 3:29









          Ben

          315




          315






















              up vote
              0
              down vote













              In all the browsers I know of, they do execute even while the console is closed - for example, try running the following code with the console closed, then open the console after 5 seconds, and you'll see that the console has indeed been populated with the text:






              setInterval(() => 
              console.log('log');
              , 1000);





              The performance impact of console.log is next to nothing, but if you wanted an easy way to toggle between having console.log calls print to the console, and do nothing, you can define a console variable on the top level of your userscript that has a log function which does nothing, thus shadowing window.console.log inside the scope of your userscript:






              const console = log: () => void 0 ;

              console.log('foo');





              To print to the console normally again, just comment out the const console... line (or use a boolean variable doPrintToConsole or something).



              This will not prevent native page scripts from console.logging. (If you wanted to prevent native page scripts from doing so, overwrite window.console.log instead);






              share|improve this answer
























                up vote
                0
                down vote













                In all the browsers I know of, they do execute even while the console is closed - for example, try running the following code with the console closed, then open the console after 5 seconds, and you'll see that the console has indeed been populated with the text:






                setInterval(() => 
                console.log('log');
                , 1000);





                The performance impact of console.log is next to nothing, but if you wanted an easy way to toggle between having console.log calls print to the console, and do nothing, you can define a console variable on the top level of your userscript that has a log function which does nothing, thus shadowing window.console.log inside the scope of your userscript:






                const console = log: () => void 0 ;

                console.log('foo');





                To print to the console normally again, just comment out the const console... line (or use a boolean variable doPrintToConsole or something).



                This will not prevent native page scripts from console.logging. (If you wanted to prevent native page scripts from doing so, overwrite window.console.log instead);






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  In all the browsers I know of, they do execute even while the console is closed - for example, try running the following code with the console closed, then open the console after 5 seconds, and you'll see that the console has indeed been populated with the text:






                  setInterval(() => 
                  console.log('log');
                  , 1000);





                  The performance impact of console.log is next to nothing, but if you wanted an easy way to toggle between having console.log calls print to the console, and do nothing, you can define a console variable on the top level of your userscript that has a log function which does nothing, thus shadowing window.console.log inside the scope of your userscript:






                  const console = log: () => void 0 ;

                  console.log('foo');





                  To print to the console normally again, just comment out the const console... line (or use a boolean variable doPrintToConsole or something).



                  This will not prevent native page scripts from console.logging. (If you wanted to prevent native page scripts from doing so, overwrite window.console.log instead);






                  share|improve this answer












                  In all the browsers I know of, they do execute even while the console is closed - for example, try running the following code with the console closed, then open the console after 5 seconds, and you'll see that the console has indeed been populated with the text:






                  setInterval(() => 
                  console.log('log');
                  , 1000);





                  The performance impact of console.log is next to nothing, but if you wanted an easy way to toggle between having console.log calls print to the console, and do nothing, you can define a console variable on the top level of your userscript that has a log function which does nothing, thus shadowing window.console.log inside the scope of your userscript:






                  const console = log: () => void 0 ;

                  console.log('foo');





                  To print to the console normally again, just comment out the const console... line (or use a boolean variable doPrintToConsole or something).



                  This will not prevent native page scripts from console.logging. (If you wanted to prevent native page scripts from doing so, overwrite window.console.log instead);






                  setInterval(() => 
                  console.log('log');
                  , 1000);





                  setInterval(() => 
                  console.log('log');
                  , 1000);





                  const console = log: () => void 0 ;

                  console.log('foo');





                  const console = log: () => void 0 ;

                  console.log('foo');






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 at 3:33









                  CertainPerformance

                  72.5k143453




                  72.5k143453



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • 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%2fstackoverflow.com%2fquestions%2f53255588%2fdoes-console-log-only-execute-when-you-use-inspect-on-website%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

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      政党

                      天津地下鉄3号線