[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-th-1-1-all-heap-and-priority-queue-in-go-and-javascript-all--*":3,"academy-blog-translations-mgybxftbhhias43":79},{"data":4,"page":78,"perPage":78,"totalItems":78,"totalPages":78},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":72,"keywords":73,"locale":49,"published_at":74,"scheduled_at":13,"school_blog":70,"short_description":75,"slug":76,"status":68,"title":6,"updated":77,"updated_by":13,"views":71},"JS2GO EP.34 การจัดการ Heap และ Priority Queue ใน Go และ JavaScript","sclblg987654321","school_blog_translations","\u003Cp>เมื่อระบบของคุณต้องจัดลำดับการทำงานของ task เช่น Job Queue, Pathfinding (A*), หรือ Recommendation Engine คุณจำเป็นต้องมีโครงสร้างข้อมูลที่สามารถ “เลือกข้อมูลที่สำคัญที่สุดก่อน” ได้อย่างมีประสิทธิภาพ และนั่นคือเหตุผลที่ Heap และ Priority Queue เข้ามามีบทบาทสำคัญในโลกของการพัฒนาโปรแกรม\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ในบทความนี้ เราจะอธิบายหลักการทำงานของ Heap และ Priority Queue พร้อม ตัวอย่างโค้ดจริงทั้งใน JavaScript และ Go\u003Cbr>รวมถึง แนวทางปฏิบัติ (Best Practices) สำหรับใช้งานในระบบจริง 🚀\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>1. ทำความเข้าใจกับ Heap\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Heap คือโครงสร้างข้อมูลแบบ Tree ที่ใช้ในการจัดลำดับข้อมูลตาม “ลำดับความสำคัญ” (priority)\u003Cbr>โดยทั่วไปแบ่งออกเป็น 2 ประเภทหลัก:\u003C\u002Fp>\u003Cul>\u003Cli>Min-Heap: ค่าที่เล็กที่สุดจะอยู่บนสุดของโครงสร้าง\u003C\u002Fli>\u003Cli>Max-Heap: ค่าที่ใหญ่ที่สุดจะอยู่บนสุดของโครงสร้าง\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Heap ถูกใช้อย่างแพร่หลายในระบบที่ต้องการ Priority Scheduling, Shortest Path (Dijkstra), หรือ Task Queue ที่ต้องเลือก task สำคัญก่อนเสมอ\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>2. การใช้งาน Heap ใน JavaScript\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>JavaScript ไม่มี Heap เป็น built-in data structure แต่สามารถสร้างขึ้นเองได้ง่าย ๆ ด้วย \u003Ccode inline=\"\">class\u003C\u002Fcode> หรือใช้ library เช่น \u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.npmjs.com\u002Fpackage\u002Fheap-js\">\u003Ccode inline=\"\">heap-js\u003C\u002Fcode>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>🔹 ตัวอย่าง: การสร้าง Min-Heap ด้วย JavaScript\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-javascript\">class MinHeap {\n  constructor() {\n    this.heap = [];\n  }\n\n  getParentIndex(i) { return Math.floor((i - 1) \u002F 2); }\n  getLeftChildIndex(i) { return 2 * i + 1; }\n  getRightChildIndex(i) { return 2 * i + 2; }\n\n  swap(i1, i2) {\n    [this.heap[i1], this.heap[i2]] = [this.heap[i2], this.heap[i1]];\n  }\n\n  insert(value) {\n    this.heap.push(value);\n    this.heapifyUp();\n  }\n\n  heapifyUp() {\n    let index = this.heap.length - 1;\n    while (this.getParentIndex(index) &gt;= 0 &amp;&amp;\n           this.heap[this.getParentIndex(index)] &gt; this.heap[index]) {\n      this.swap(index, this.getParentIndex(index));\n      index = this.getParentIndex(index);\n    }\n  }\n\n  extractMin() {\n    if (this.heap.length === 0) return null;\n    if (this.heap.length === 1) return this.heap.pop();\n\n    const root = this.heap[0];\n    this.heap[0] = this.heap.pop();\n    this.heapifyDown(0);\n    return root;\n  }\n\n  heapifyDown(index) {\n    let smallest = index;\n    const left = this.getLeftChildIndex(index);\n    const right = this.getRightChildIndex(index);\n\n    if (left &lt; this.heap.length &amp;&amp; this.heap[left] &lt; this.heap[smallest]) smallest = left;\n    if (right &lt; this.heap.length &amp;&amp; this.heap[right] &lt; this.heap[smallest]) smallest = right;\n\n    if (smallest !== index) {\n      this.swap(index, smallest);\n      this.heapifyDown(smallest);\n    }\n  }\n}\n\n\u002F\u002F ทดสอบการใช้งาน\nconst minHeap = new MinHeap();\nminHeap.insert(5);\nminHeap.insert(2);\nminHeap.insert(8);\nminHeap.insert(1);\n\nconsole.log(minHeap.extractMin()); \u002F\u002F 1\nconsole.log(minHeap.extractMin()); \u002F\u002F 2\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ผลลัพธ์:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">1\n2\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ข้อดี:\u003Cbr>✅ เข้าใจง่ายและสามารถ customize ได้\u003Cbr>✅ เหมาะสำหรับฝึกอัลกอริทึม หรือใช้ในระบบ simulation\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ข้อจำกัด:\u003Cbr>❌ ต้อง implement เอง\u003Cbr>❌ ไม่มี type safety\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>3. การใช้งาน Heap และ Priority Queue ใน Go\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ภาษา Go มี package มาตรฐานชื่อ \u003Ccode inline=\"\">container\u002Fheap\u003C\u002Fcode> ที่ช่วยให้เราสามารถจัดการ heap ได้โดยตรงและปลอดภัยด้วย type checking\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>🔹 ตัวอย่าง: การสร้าง Priority Queue ใน Go\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport (\n\t\"container\u002Fheap\"\n\t\"fmt\"\n)\n\n\u002F\u002F Item แทน task ที่มี priority\ntype Item struct {\n\tvalue    string\n\tpriority int\n\tindex    int\n}\n\n\u002F\u002F PriorityQueue คือ heap ของ Item\ntype PriorityQueue []*Item\n\nfunc (pq PriorityQueue) Len() int { return len(pq) }\nfunc (pq PriorityQueue) Less(i, j int) bool {\n\treturn pq[i].priority &lt; pq[j].priority \u002F\u002F Min-Heap\n}\nfunc (pq PriorityQueue) Swap(i, j int) {\n\tpq[i], pq[j] = pq[j], pq[i]\n\tpq[i].index = i\n\tpq[j].index = j\n}\nfunc (pq *PriorityQueue) Push(x any) {\n\titem := x.(*Item)\n\titem.index = len(*pq)\n\t*pq = append(*pq, item)\n}\nfunc (pq *PriorityQueue) Pop() any {\n\told := *pq\n\tn := len(old)\n\titem := old[n-1]\n\t*pq = old[0 : n-1]\n\treturn item\n}\n\nfunc main() {\n\tpq := make(PriorityQueue, 0)\n\theap.Init(&amp;pq)\n\n\theap.Push(&amp;pq, &amp;Item{value: \"Job A\", priority: 3})\n\theap.Push(&amp;pq, &amp;Item{value: \"Job B\", priority: 1})\n\theap.Push(&amp;pq, &amp;Item{value: \"Job C\", priority: 2})\n\n\tfor pq.Len() &gt; 0 {\n\t\titem := heap.Pop(&amp;pq).(*Item)\n\t\tfmt.Printf(\"%s (priority %d)\\n\", item.value, item.priority)\n\t}\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ผลลัพธ์:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">Job B (priority 1)\nJob C (priority 2)\nJob A (priority 3)\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ข้อดีของ Go:\u003Cbr>✅ มี \u003Ccode inline=\"\">heap\u003C\u002Fcode> package ให้ใช้งานได้ทันที\u003Cbr>✅ Type-safe และมีประสิทธิภาพสูง\u003Cbr>✅ เหมาะกับระบบ production เช่น job queue, scheduler, และ background worker\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>4. แนวทางปฏิบัติที่เหมาะสม (Best Practices)\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cfigure class=\"table\">\u003Ctable>\u003Cthead>\u003Ctr>\u003Cth>Use Case\u003C\u002Fth>\u003Cth>เหมาะกับภาษา\u003C\u002Fth>\u003Cth>คำแนะนำ\u003C\u002Fth>\u003C\u002Ftr>\u003C\u002Fthead>\u003Ctbody>\u003Ctr>\u003Ctd>Algorithm Simulation\u003C\u002Ftd>\u003Ctd>JavaScript\u003C\u002Ftd>\u003Ctd>เขียน heap เองเพื่อฝึกเข้าใจอัลกอริทึมพื้นฐาน\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Production Queue System\u003C\u002Ftd>\u003Ctd>Go\u003C\u002Ftd>\u003Ctd>ใช้ \u003Ccode inline=\"\">container\u002Fheap\u003C\u002Fcode> เพื่อประสิทธิภาพสูงสุด\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Pathfinding (A*, Dijkstra)\u003C\u002Ftd>\u003Ctd>ทั้งสองภาษา\u003C\u002Ftd>\u003Ctd>ใช้ Priority Queue เพื่อหาทางที่สั้นที่สุด\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Task Scheduling \u002F Job Management\u003C\u002Ftd>\u003Ctd>Go\u003C\u002Ftd>\u003Ctd>ใช้ Priority Queue เพื่อควบคุมลำดับงานอย่างมีประสิทธิภาพ\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003C\u002Ffigure>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>5. สรุปเปรียบเทียบ Heap และ Priority Queue\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cfigure class=\"table\">\u003Ctable>\u003Cthead>\u003Ctr>\u003Cth>คุณสมบัติ\u003C\u002Fth>\u003Cth>JavaScript\u003C\u002Fth>\u003Cth>Go\u003C\u002Fth>\u003C\u002Ftr>\u003C\u002Fthead>\u003Ctbody>\u003Ctr>\u003Ctd>Built-in Support\u003C\u002Ftd>\u003Ctd>❌ ไม่มี\u003C\u002Ftd>\u003Ctd>✅ มี (\u003Ccode inline=\"\">container\u002Fheap\u003C\u002Fcode>)\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Type Safety\u003C\u002Ftd>\u003Ctd>❌ ไม่มี\u003C\u002Ftd>\u003Ctd>✅ มี\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Performance\u003C\u002Ftd>\u003Ctd>ปานกลาง\u003C\u002Ftd>\u003Ctd>สูงมาก\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Suitable For\u003C\u002Ftd>\u003Ctd>ฝึก algorithm, simulation\u003C\u002Ftd>\u003Ctd>ระบบจริง, job scheduling\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Ease of Use\u003C\u002Ftd>\u003Ctd>เข้าใจง่าย\u003C\u002Ftd>\u003Ctd>ต้อง implement interface\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003C\u002Ffigure>\u003Cp>&nbsp;\u003C\u002Fp>\u003Chr>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>สรุปท้ายบท\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>การเข้าใจการทำงานของ Heap และ Priority Queue จะช่วยให้คุณสามารถสร้างระบบที่จัดลำดับความสำคัญของงานได้อย่างมีประสิทธิภาพ ไม่ว่าจะเป็น Job Queue, Pathfinding Engine, หรือ Task Manager\u003C\u002Fp>\u003Cul>\u003Cli>💡 ถ้าคุณต้องการ “ความยืดหยุ่นและเข้าใจพื้นฐานอัลกอริทึม” → ใช้ JavaScript\u003C\u002Fli>\u003Cli>⚙️ ถ้าคุณต้องการ “ประสิทธิภาพระดับ Production และระบบคิวที่แข็งแรง” → ใช้ Go\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>ตอนต่อไป\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ใน EP.35 ของซีรีส์ JS2GO เราจะพาคุณไปเรียนรู้เรื่อง Sorting Algorithms ทั้งใน JavaScript และ Go เพื่อเปรียบเทียบวิธีการจัดเรียงข้อมูล เช่น Bubble Sort, Merge Sort, Quick Sort, และการใช้งาน Go’s \u003Ccode inline=\"\">sort\u003C\u002Fcode> package ว่าภาษาไหนทำได้เร็วกว่ากัน และเทคนิคไหนเหมาะกับระบบของคุณมากที่สุด 🔥\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cstrong>อ่านบทความ Series อื่นๆ\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul>\u003Cli>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FGolang\">\u003Cstrong>Golang The Series\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fli>\u003Cli>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FJS2GO\">\u003Cstrong>JS2GO\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fli>\u003Cli>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FTailwind%20CSS\">\u003Cstrong>10 Ep ที่จะให้คุณเป็นมือโปร Tailwind CSS ในชั่วข้ามคืน\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fli>\u003C\u002Ful>\u003Cp>\u003Cstrong>🔵 Facebook: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.facebook.com\u002Fsuperdev.academy.th\">\u003Cstrong>https:\u002F\u002Fwww.facebook.com\u002Fsuperdev.academy.th\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🔴 YouTube : \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.youtube.com\u002Fchannel\u002FUC2eI2RxcA2zbTqZyHyWIfRg\">\u003Cstrong>Superdev Academy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>📸 Instagram: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.instagram.com\u002Fsuperdevschool\u002F\">\u003Cstrong>Superdev Academy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🎬 TikTok: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\">\u003Cstrong>https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🌐 Website: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdevacademy.com\u002F\">\u003Cstrong>https:\u002F\u002Fwww.superdevacademy.com\u002F\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>","67_11zon_p6wwrz8255.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fn9pt1j9ian4j2md\u002F67_11zon_p6wwrz8255.webp","2026-03-04 08:45:41.345Z","",{"keywords":15,"locale":43,"school_blog":53},[16,23,28,33,38],{"collectionId":17,"collectionName":18,"created":19,"created_by":13,"id":20,"name":21,"updated":22,"updated_by":13},"sclkey987654321","school_keywords","2026-03-04 08:45:40.772Z","v6z1lw61iahm61l","Heap","2026-04-10 16:13:01.678Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:45:40.568Z","5g4t5pkbuw4nirz","Priority Queue","2026-04-10 16:13:01.534Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:20:42.484Z","vslzz9nvv6n77cx","JavaScript","2026-04-10 16:07:28.339Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:45:30.765Z","nayuhpre9emd5xw","JS2GO","2026-04-10 16:12:59.160Z",{"code":44,"collectionId":45,"collectionName":46,"created":47,"flag":48,"id":49,"is_default":50,"label":51,"updated":52},"th","pbc_1989393366","locales","2026-01-22 10:59:55.832Z","twemoji:flag-thailand","s8wri3bt4vgg2ji",true,"Thai","2026-04-10 15:42:46.614Z",{"category":54,"collectionId":55,"collectionName":56,"expand":57,"id":70,"views":71},"hsa1afr8fcnd6qb","pbc_2105096300","school_blogs",{"category":58},{"blogIds":59,"collectionId":60,"collectionName":61,"created":62,"created_by":13,"id":54,"image":63,"image_alt":13,"image_path":64,"label":65,"name":41,"priority":66,"publish_at":67,"scheduled_at":13,"status":68,"updated":69,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-04 08:24:37.986Z","js2_go_2_11zon_y6paxmuz32.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fhsa1afr8fcnd6qb\u002Fjs2_go_2_11zon_y6paxmuz32.webp",{"en":41,"th":41},10,"2025-08-11 03:41:08.820Z","published","2026-04-25 02:32:14.339Z","mgybxftbhhias43",226,"n9pt1j9ian4j2md",[20,25,30,35,40],"2025-11-03 01:38:54.986Z","Heap และ Priority Queue คือหัวใจของระบบที่ต้องจัดลำดับงาน เช่น Job Queue, Pathfinding หรือ Recommendation Engine","heap-and-priority-queue-in-go-and-javascript","2026-04-25 02:47:55.951Z",1,{"th":76}]