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);
javascript timeout settimeout
|
show 3 more comments
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);
javascript timeout settimeout
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
|
show 3 more comments
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);
javascript timeout settimeout
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
javascript timeout settimeout
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
|
show 3 more comments
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
|
show 3 more comments
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.
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).
add a comment |
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
add a comment |
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.
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).
add a comment |
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.
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).
add a comment |
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.
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).
My tests show that method 2 is faster. I repeated the test about 10 times. I used the Chrome Performance Dev Tool.
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);
}
answered Nov 9 at 11:20
Neskews
719
719
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 9 at 11:22
Nelson Owalo
1,017925
1,017925
add a comment |
add a comment |
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%2fstackoverflow.com%2fquestions%2f53224539%2ftimeout-chaining-method%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
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