Skip to content

Instantly share code, notes, and snippets.

@ywwwtseng
Last active April 20, 2025 08:58
Show Gist options
  • Select an option

  • Save ywwwtseng/e9c62c7f89a78049cc93f950ec2ea711 to your computer and use it in GitHub Desktop.

Select an option

Save ywwwtseng/e9c62c7f89a78049cc93f950ec2ea711 to your computer and use it in GitHub Desktop.

Revisions

  1. ywwwtseng revised this gist Apr 19, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion event-loop-queue-vs-job-queue.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ## 🌀 Event Loop Queue vs Job Queue 差異說明
    # 🌀 Event Loop Queue vs Job Queue 差異說明

    | 項目 | Event Loop Queue(Macro Task Queue) | Job Queue(Micro Task Queue) |
    |------|----------------------------------------|-------------------------------|
  2. ywwwtseng revised this gist Apr 19, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion event-loop-queue-vs-job-queue.md
    Original file line number Diff line number Diff line change
    @@ -39,4 +39,4 @@ console.log('2'); // 同步
    ```

    ### 參考
    [JavaScript execution model](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Execution_model#job_queue_and_event_loop)
    [JavaScript execution model](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Execution_model)
  3. ywwwtseng revised this gist Apr 19, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion event-loop-queue-vs-job-queue.md
    Original file line number Diff line number Diff line change
    @@ -39,4 +39,4 @@ console.log('2'); // 同步
    ```

    ### 參考
    ![JavaScript execution model](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Execution_model#job_queue_and_event_loop)
    [JavaScript execution model](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Execution_model#job_queue_and_event_loop)
  4. ywwwtseng revised this gist Apr 19, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion event-loop-queue-vs-job-queue.md
    Original file line number Diff line number Diff line change
    @@ -39,4 +39,4 @@ console.log('2'); // 同步
    ```

    ### 參考
    !(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Execution_model#job_queue_and_event_loop)[JavaScript execution model]
    ![JavaScript execution model](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Execution_model#job_queue_and_event_loop)
  5. ywwwtseng revised this gist Apr 19, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion event-loop-queue-vs-job-queue.md
    Original file line number Diff line number Diff line change
    @@ -39,4 +39,4 @@ console.log('2'); // 同步
    ```

    ### 參考
    ![https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Execution_model#job_queue_and_event_loop](JavaScript execution model)
    !(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Execution_model#job_queue_and_event_loop)[JavaScript execution model]
  6. ywwwtseng revised this gist Apr 19, 2025. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion event-loop-queue-vs-job-queue.md
    Original file line number Diff line number Diff line change
    @@ -36,4 +36,7 @@ Promise.resolve().then(() => {
    }); // 放入 Job Queue

    console.log('2'); // 同步
    ```
    ```

    ### 參考
    ![https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Execution_model#job_queue_and_event_loop](JavaScript execution model)
  7. ywwwtseng revised this gist Apr 19, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion event-loop-queue-vs-job-queue.md
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@
    ... 如此循環
    ```

    ### 範例
    ### 🧪 範例

    ```js
    console.log('1'); // 同步
  8. ywwwtseng revised this gist Apr 19, 2025. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions event-loop-queue-vs-job-queue.md
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,8 @@
    | 🥇 優先順序 | 較低,等微任務執行完才會輪到它 | 較高,會優先執行完所有 microtasks 才會進入下一個 macro task |
    | ✅ 用途 | 處理整體非同步任務 | 處理細粒度更新、異步邏輯鏈接 |

    ### 📊 執行順序圖(事件循環階段)

    ```
    [ 呼叫堆疊空了 ]
    @@ -18,4 +20,20 @@
    [ 清空 Microtasks ]
    ... 如此循環
    ```

    ### 範例

    ```js
    console.log('1'); // 同步

    setTimeout(() => {
    console.log('4');
    }, 0); // 放入 Task Queue

    Promise.resolve().then(() => {
    console.log('3');
    }); // 放入 Job Queue

    console.log('2'); // 同步
    ```
  9. ywwwtseng revised this gist Apr 19, 2025. 1 changed file with 13 additions and 1 deletion.
    14 changes: 13 additions & 1 deletion event-loop-queue-vs-job-queue.md
    Original file line number Diff line number Diff line change
    @@ -6,4 +6,16 @@
    | 🎯 負責處理 | setTimeout、setInterval、I/O callbacks、DOM Events 等 | Promise.then()、queueMicrotask()、MutationObserver |
    | ⏳ 執行時機 | 每輪事件循環的最後階段才會執行 | 每個 task 執行結束後立即清空 microtasks |
    | 🥇 優先順序 | 較低,等微任務執行完才會輪到它 | 較高,會優先執行完所有 microtasks 才會進入下一個 macro task |
    | ✅ 用途 | 處理整體非同步任務 | 處理細粒度更新、異步邏輯鏈接 |
    | ✅ 用途 | 處理整體非同步任務 | 處理細粒度更新、異步邏輯鏈接 |

    ```
    [ 呼叫堆疊空了 ]
    [ 清空所有 Microtasks(Job Queue) ]
    [ 執行下一個 Macro Task(Event Loop Queue) ]
    [ 清空 Microtasks ]
    ... 如此循環
    ```
  10. ywwwtseng created this gist Apr 19, 2025.
    9 changes: 9 additions & 0 deletions event-loop-queue-vs-job-queue.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    ## 🌀 Event Loop Queue vs Job Queue 差異說明

    | 項目 | Event Loop Queue(Macro Task Queue) | Job Queue(Micro Task Queue) |
    |------|----------------------------------------|-------------------------------|
    | 📌 又稱 | Task Queue / Callback Queue | Microtask Queue / Job Queue |
    | 🎯 負責處理 | setTimeout、setInterval、I/O callbacks、DOM Events 等 | Promise.then()、queueMicrotask()、MutationObserver |
    | ⏳ 執行時機 | 每輪事件循環的最後階段才會執行 | 每個 task 執行結束後立即清空 microtasks |
    | 🥇 優先順序 | 較低,等微任務執行完才會輪到它 | 較高,會優先執行完所有 microtasks 才會進入下一個 macro task |
    | ✅ 用途 | 處理整體非同步任務 | 處理細粒度更新、異步邏輯鏈接 |