Timeout Chaining Method











up vote
0
down vote

favorite












So I have a question regarding chaining timeouts. I have 2 methods here which I am deciding what to use from. Method 1 is definitely a lot cleaner, however I am not sure if it is less accurate or slower in terms of performance than the other?



Method 1:



setTimeout(() => {

}, 1 * 60000);

setTimeout(() => {

}, 2 * 60000);

setTimeout(() => {

}, 3 * 60000);


Method 2:



setTimeout(() => {
setTimeout(() => {
setTimeout(() => {

}, 1 * 60000);
}, 1 * 60000);
}, 1 * 60000);









share|improve this question






















  • completely out of curiosity, where will you apply this?
    – Nelson Owalo
    Nov 9 at 11:10










  • It will be used for a countdown, with different logic inside each setTimeout (so setIntervals can't be used).
    – Luke
    Nov 9 at 11:11










  • Method 2 is recomended... Let me run some tests and see
    – Nelson Owalo
    Nov 9 at 11:12












  • In terms of performance those two are pretty identical
    – hindmost
    Nov 9 at 11:13










  • @hindmost I kind of doubt.. In method 1, aren't all timeouts gonna be called at once (same time)? Compared to method 2?
    – Nelson Owalo
    Nov 9 at 11:16















up vote
0
down vote

favorite












So I have a question regarding chaining timeouts. I have 2 methods here which I am deciding what to use from. Method 1 is definitely a lot cleaner, however I am not sure if it is less accurate or slower in terms of performance than the other?



Method 1:



setTimeout(() => {

}, 1 * 60000);

setTimeout(() => {

}, 2 * 60000);

setTimeout(() => {

}, 3 * 60000);


Method 2:



setTimeout(() => {
setTimeout(() => {
setTimeout(() => {

}, 1 * 60000);
}, 1 * 60000);
}, 1 * 60000);









share|improve this question






















  • completely out of curiosity, where will you apply this?
    – Nelson Owalo
    Nov 9 at 11:10










  • It will be used for a countdown, with different logic inside each setTimeout (so setIntervals can't be used).
    – Luke
    Nov 9 at 11:11










  • Method 2 is recomended... Let me run some tests and see
    – Nelson Owalo
    Nov 9 at 11:12












  • In terms of performance those two are pretty identical
    – hindmost
    Nov 9 at 11:13










  • @hindmost I kind of doubt.. In method 1, aren't all timeouts gonna be called at once (same time)? Compared to method 2?
    – Nelson Owalo
    Nov 9 at 11:16













up vote
0
down vote

favorite









up vote
0
down vote

favorite











So I have a question regarding chaining timeouts. I have 2 methods here which I am deciding what to use from. Method 1 is definitely a lot cleaner, however I am not sure if it is less accurate or slower in terms of performance than the other?



Method 1:



setTimeout(() => {

}, 1 * 60000);

setTimeout(() => {

}, 2 * 60000);

setTimeout(() => {

}, 3 * 60000);


Method 2:



setTimeout(() => {
setTimeout(() => {
setTimeout(() => {

}, 1 * 60000);
}, 1 * 60000);
}, 1 * 60000);









share|improve this question













So I have a question regarding chaining timeouts. I have 2 methods here which I am deciding what to use from. Method 1 is definitely a lot cleaner, however I am not sure if it is less accurate or slower in terms of performance than the other?



Method 1:



setTimeout(() => {

}, 1 * 60000);

setTimeout(() => {

}, 2 * 60000);

setTimeout(() => {

}, 3 * 60000);


Method 2:



setTimeout(() => {
setTimeout(() => {
setTimeout(() => {

}, 1 * 60000);
}, 1 * 60000);
}, 1 * 60000);






javascript timeout settimeout






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 11:05









Luke

136




136












  • completely out of curiosity, where will you apply this?
    – Nelson Owalo
    Nov 9 at 11:10










  • It will be used for a countdown, with different logic inside each setTimeout (so setIntervals can't be used).
    – Luke
    Nov 9 at 11:11










  • Method 2 is recomended... Let me run some tests and see
    – Nelson Owalo
    Nov 9 at 11:12












  • In terms of performance those two are pretty identical
    – hindmost
    Nov 9 at 11:13










  • @hindmost I kind of doubt.. In method 1, aren't all timeouts gonna be called at once (same time)? Compared to method 2?
    – Nelson Owalo
    Nov 9 at 11:16


















  • completely out of curiosity, where will you apply this?
    – Nelson Owalo
    Nov 9 at 11:10










  • It will be used for a countdown, with different logic inside each setTimeout (so setIntervals can't be used).
    – Luke
    Nov 9 at 11:11










  • Method 2 is recomended... Let me run some tests and see
    – Nelson Owalo
    Nov 9 at 11:12












  • In terms of performance those two are pretty identical
    – hindmost
    Nov 9 at 11:13










  • @hindmost I kind of doubt.. In method 1, aren't all timeouts gonna be called at once (same time)? Compared to method 2?
    – Nelson Owalo
    Nov 9 at 11:16
















completely out of curiosity, where will you apply this?
– Nelson Owalo
Nov 9 at 11:10




completely out of curiosity, where will you apply this?
– Nelson Owalo
Nov 9 at 11:10












It will be used for a countdown, with different logic inside each setTimeout (so setIntervals can't be used).
– Luke
Nov 9 at 11:11




It will be used for a countdown, with different logic inside each setTimeout (so setIntervals can't be used).
– Luke
Nov 9 at 11:11












Method 2 is recomended... Let me run some tests and see
– Nelson Owalo
Nov 9 at 11:12






Method 2 is recomended... Let me run some tests and see
– Nelson Owalo
Nov 9 at 11:12














In terms of performance those two are pretty identical
– hindmost
Nov 9 at 11:13




In terms of performance those two are pretty identical
– hindmost
Nov 9 at 11:13












@hindmost I kind of doubt.. In method 1, aren't all timeouts gonna be called at once (same time)? Compared to method 2?
– Nelson Owalo
Nov 9 at 11:16




@hindmost I kind of doubt.. In method 1, aren't all timeouts gonna be called at once (same time)? Compared to method 2?
– Nelson Owalo
Nov 9 at 11:16












2 Answers
2






active

oldest

votes

















up vote
0
down vote













My tests show that method 2 is faster. I repeated the test about 10 times. I used the Chrome Performance Dev Tool.



enter image description hereenter image description here



I've used this code:






function runFirst() {
setTimeout(() => {
console.log(1);
}, 1000);

setTimeout(() => {
console.log(2);
}, 2 * 1000);
}

function runSecond() {
setTimeout(() => {
console.log(3);

setTimeout(() => {
console.log(4);
}, 1000);
}, 1000);
}





However, these difference are really minimal. I'd recommend using the code that is better readable (method 1).






share|improve this answer




























    up vote
    0
    down vote













    Method 2 is faster. Here is the code I tested: Benchmark



    // function1
    function func1() {
    setTimeout(() => { }, 1 * 60000);
    setTimeout(() => { }, 2 * 60000);
    setTimeout(() => { }, 3 * 60000);
    return 'done'
    }

    func1()

    // function2
    function func2() {
    setTimeout(() => {
    setTimeout(() => {
    setTimeout(() => { }, 1 * 60000);
    }, 1 * 60000);
    }, 1 * 60000);
    return 'done';
    }

    func2()


    Here are the results






    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%2f53224539%2ftimeout-chaining-method%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













      My tests show that method 2 is faster. I repeated the test about 10 times. I used the Chrome Performance Dev Tool.



      enter image description hereenter image description here



      I've used this code:






      function runFirst() {
      setTimeout(() => {
      console.log(1);
      }, 1000);

      setTimeout(() => {
      console.log(2);
      }, 2 * 1000);
      }

      function runSecond() {
      setTimeout(() => {
      console.log(3);

      setTimeout(() => {
      console.log(4);
      }, 1000);
      }, 1000);
      }





      However, these difference are really minimal. I'd recommend using the code that is better readable (method 1).






      share|improve this answer

























        up vote
        0
        down vote













        My tests show that method 2 is faster. I repeated the test about 10 times. I used the Chrome Performance Dev Tool.



        enter image description hereenter image description here



        I've used this code:






        function runFirst() {
        setTimeout(() => {
        console.log(1);
        }, 1000);

        setTimeout(() => {
        console.log(2);
        }, 2 * 1000);
        }

        function runSecond() {
        setTimeout(() => {
        console.log(3);

        setTimeout(() => {
        console.log(4);
        }, 1000);
        }, 1000);
        }





        However, these difference are really minimal. I'd recommend using the code that is better readable (method 1).






        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          My tests show that method 2 is faster. I repeated the test about 10 times. I used the Chrome Performance Dev Tool.



          enter image description hereenter image description here



          I've used this code:






          function runFirst() {
          setTimeout(() => {
          console.log(1);
          }, 1000);

          setTimeout(() => {
          console.log(2);
          }, 2 * 1000);
          }

          function runSecond() {
          setTimeout(() => {
          console.log(3);

          setTimeout(() => {
          console.log(4);
          }, 1000);
          }, 1000);
          }





          However, these difference are really minimal. I'd recommend using the code that is better readable (method 1).






          share|improve this answer












          My tests show that method 2 is faster. I repeated the test about 10 times. I used the Chrome Performance Dev Tool.



          enter image description hereenter image description here



          I've used this code:






          function runFirst() {
          setTimeout(() => {
          console.log(1);
          }, 1000);

          setTimeout(() => {
          console.log(2);
          }, 2 * 1000);
          }

          function runSecond() {
          setTimeout(() => {
          console.log(3);

          setTimeout(() => {
          console.log(4);
          }, 1000);
          }, 1000);
          }





          However, these difference are really minimal. I'd recommend using the code that is better readable (method 1).






          function runFirst() {
          setTimeout(() => {
          console.log(1);
          }, 1000);

          setTimeout(() => {
          console.log(2);
          }, 2 * 1000);
          }

          function runSecond() {
          setTimeout(() => {
          console.log(3);

          setTimeout(() => {
          console.log(4);
          }, 1000);
          }, 1000);
          }





          function runFirst() {
          setTimeout(() => {
          console.log(1);
          }, 1000);

          setTimeout(() => {
          console.log(2);
          }, 2 * 1000);
          }

          function runSecond() {
          setTimeout(() => {
          console.log(3);

          setTimeout(() => {
          console.log(4);
          }, 1000);
          }, 1000);
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 9 at 11:20









          Neskews

          719




          719
























              up vote
              0
              down vote













              Method 2 is faster. Here is the code I tested: Benchmark



              // function1
              function func1() {
              setTimeout(() => { }, 1 * 60000);
              setTimeout(() => { }, 2 * 60000);
              setTimeout(() => { }, 3 * 60000);
              return 'done'
              }

              func1()

              // function2
              function func2() {
              setTimeout(() => {
              setTimeout(() => {
              setTimeout(() => { }, 1 * 60000);
              }, 1 * 60000);
              }, 1 * 60000);
              return 'done';
              }

              func2()


              Here are the results






              share|improve this answer

























                up vote
                0
                down vote













                Method 2 is faster. Here is the code I tested: Benchmark



                // function1
                function func1() {
                setTimeout(() => { }, 1 * 60000);
                setTimeout(() => { }, 2 * 60000);
                setTimeout(() => { }, 3 * 60000);
                return 'done'
                }

                func1()

                // function2
                function func2() {
                setTimeout(() => {
                setTimeout(() => {
                setTimeout(() => { }, 1 * 60000);
                }, 1 * 60000);
                }, 1 * 60000);
                return 'done';
                }

                func2()


                Here are the results






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Method 2 is faster. Here is the code I tested: Benchmark



                  // function1
                  function func1() {
                  setTimeout(() => { }, 1 * 60000);
                  setTimeout(() => { }, 2 * 60000);
                  setTimeout(() => { }, 3 * 60000);
                  return 'done'
                  }

                  func1()

                  // function2
                  function func2() {
                  setTimeout(() => {
                  setTimeout(() => {
                  setTimeout(() => { }, 1 * 60000);
                  }, 1 * 60000);
                  }, 1 * 60000);
                  return 'done';
                  }

                  func2()


                  Here are the results






                  share|improve this answer












                  Method 2 is faster. Here is the code I tested: Benchmark



                  // function1
                  function func1() {
                  setTimeout(() => { }, 1 * 60000);
                  setTimeout(() => { }, 2 * 60000);
                  setTimeout(() => { }, 3 * 60000);
                  return 'done'
                  }

                  func1()

                  // function2
                  function func2() {
                  setTimeout(() => {
                  setTimeout(() => {
                  setTimeout(() => { }, 1 * 60000);
                  }, 1 * 60000);
                  }, 1 * 60000);
                  return 'done';
                  }

                  func2()


                  Here are the results







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 9 at 11:22









                  Nelson Owalo

                  1,017925




                  1,017925






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224539%2ftimeout-chaining-method%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

                      Schultheiß

                      Verwaltungsgliederung Dänemarks

                      Liste der Kulturdenkmale in Wilsdruff