Skip to content

Instantly share code, notes, and snippets.

@emayom
Last active October 16, 2021 15:19
Show Gist options
  • Save emayom/e9c03ce1e5898aae2a5eba545087b2bf to your computer and use it in GitHub Desktop.
Save emayom/e9c03ce1e5898aae2a5eba545087b2bf to your computer and use it in GitHub Desktop.
sync vs async
//동기 방식으로 작동하는 코드
function loop() {
//for문을 실행하기 전
const start = Date.now();
//i가 9999999가 될 때 까지 ++
for (let i = 0; i <= 9999999; i++) {}
const end = Date.now();
//걸린 시간 => ms
console.log(`1번째 작업 완료! (${end - start}ms)`);
}
loop();
console.log('2번째 작업 완료!');
//output
//1번째 작업 완료!
//2번째 작업 완료!
//비동기 방식으로 작동하는 코드
function loop() {
setTimeout(() => {
//for문을 실행하기 전
const start = Date.now();
//i가 9999999가 될 때 까지 ++
for (let i = 0; i <= 9999999; i++) {}
const end = Date.now();
//걸린 시간 => ms
console.log(`1번째 작업 완료!(${end - start}ms)`);
},0);
}
loop();
console.log('2번째 작업 완료!');
//output
//2번째 작업 완료!
//1번째 작업 완료!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment