[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-th-1-1-all-js2go-ep36-searching-algorithms-in-go-and-javascript-all--*":3,"academy-blog-translations-zwxfag96iy33ciz":80},{"data":4,"page":79,"perPage":79,"totalItems":79,"totalPages":79},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":73,"keywords":74,"locale":49,"published_at":75,"scheduled_at":13,"school_blog":71,"short_description":76,"slug":77,"status":69,"title":6,"updated":78,"updated_by":13,"views":72},"JS2GO EP.36 การทำ Searching Algorithms ใน Go และ JavaScript","sclblg987654321","school_blog_translations","\u003Cp>การค้นหาข้อมูล (Searching) คือหนึ่งในกระบวนการพื้นฐานของทุกระบบ ไม่ว่าจะเป็น\u003C\u002Fp>\u003Cul>\u003Cli>การค้นหาผู้ใช้ในฐานข้อมูล\u003C\u002Fli>\u003Cli>การค้นหาสินค้าในร้านค้าออนไลน์\u003C\u002Fli>\u003Cli>หรือการค้นหาคำในเอกสาร\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>อัลกอริทึมการค้นหาที่ดีสามารถช่วยให้ระบบของคุณตอบสนองได้รวดเร็วและใช้ทรัพยากรอย่างมีประสิทธิภาพมากขึ้น\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ในบทความนี้ เราจะเปรียบเทียบการทำงานของ Searching Algorithms ระหว่าง JavaScript และ Go\u003Cbr>ผ่าน 3 เทคนิคหลัก ได้แก่\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>1️⃣ Linear Search (ค้นหาแบบเส้นตรง)\u003Cbr>2️⃣ Binary Search (ค้นหาแบบแบ่งครึ่ง)\u003Cbr>3️⃣ Map-based Lookup (ค้นหาแบบ Key-Value)\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>พร้อมแนวทางการเลือกใช้ให้เหมาะสมกับระบบจริง 🚀\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>1. Linear Search — ค้นหาแบบเส้นตรง\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>หลักการ:\u003Cbr>วนลูปตรวจสอบแต่ละค่าทีละตัวไปเรื่อย ๆ จนกว่าจะพบข้อมูลที่ตรงกับเป้าหมาย\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>🔹 JavaScript: Linear Search\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-javascript\">function linearSearch(arr, target) {\n  for (let i = 0; i &lt; arr.length; i++) {\n    if (arr[i] === target) return i;\n  }\n  return -1;\n}\n\nconst nums = [10, 25, 30, 45, 60];\nconsole.log(linearSearch(nums, 30));  \u002F\u002F Output: 2\nconsole.log(linearSearch(nums, 99));  \u002F\u002F Output: -1\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>🔹 Go: Linear Search\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport \"fmt\"\n\nfunc linearSearch(arr []int, target int) int {\n\tfor i, v := range arr {\n\t\tif v == target {\n\t\t\treturn i\n\t\t}\n\t}\n\treturn -1\n}\n\nfunc main() {\n\tnums := []int{10, 25, 30, 45, 60}\n\tfmt.Println(linearSearch(nums, 30)) \u002F\u002F Output: 2\n\tfmt.Println(linearSearch(nums, 99)) \u002F\u002F Output: -1\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>สรุป:\u003Cbr>✅ เข้าใจง่าย ใช้งานได้กับข้อมูลทุกประเภท\u003Cbr>❌ ช้าเมื่อต้องค้นหาในข้อมูลจำนวนมาก\u003Cbr>Complexity: O(n)\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>2. Binary Search — ค้นหาแบบแบ่งครึ่ง\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>หลักการ:\u003Cbr>ใช้ได้เฉพาะกับข้อมูลที่ “เรียงลำดับแล้ว” เท่านั้น โดยจะทำการ “แบ่งครึ่ง” ขอบเขตการค้นหาในแต่ละรอบ เพื่อลดจำนวนครั้งในการตรวจสอบ\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>🔹 JavaScript: Binary Search\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-javascript\">function binarySearch(arr, target) {\n  let left = 0;\n  let right = arr.length - 1;\n\n  while (left &lt;= right) {\n    const mid = Math.floor((left + right) \u002F 2);\n    if (arr[mid] === target) return mid;\n    if (arr[mid] &lt; target) left = mid + 1;\n    else right = mid - 1;\n  }\n\n  return -1;\n}\n\nconst sorted = [10, 25, 30, 45, 60];\nconsole.log(binarySearch(sorted, 45)); \u002F\u002F Output: 3\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>🔹 Go: Binary Search\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport \"fmt\"\n\nfunc binarySearch(arr []int, target int) int {\n\tleft, right := 0, len(arr)-1\n\tfor left &lt;= right {\n\t\tmid := (left + right) \u002F 2\n\t\tif arr[mid] == target {\n\t\t\treturn mid\n\t\t}\n\t\tif arr[mid] &lt; target {\n\t\t\tleft = mid + 1\n\t\t} else {\n\t\t\tright = mid - 1\n\t\t}\n\t}\n\treturn -1\n}\n\nfunc main() {\n\tsorted := []int{10, 25, 30, 45, 60}\n\tfmt.Println(binarySearch(sorted, 45)) \u002F\u002F Output: 3\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>สรุป:\u003Cbr>✅ ทำงานรวดเร็วมากหากข้อมูล “เรียงลำดับแล้ว”\u003Cbr>❌ ใช้ไม่ได้กับข้อมูลที่ยังไม่เรียงลำดับ\u003Cbr>Complexity: O(log n)\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>3. Map-based Lookup — การค้นหาแบบ Key-Value\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>หลักการ:\u003Cbr>แทนที่จะค้นหาทีละตัว ระบบจะเก็บข้อมูลในรูปแบบ Key → Value เมื่อค้นหา สามารถเข้าถึงข้อมูลได้โดยตรงในเวลา O(1)\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>🔹 JavaScript: Using Map\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-javascript\">const userMap = new Map();\nuserMap.set(\"john\", { age: 25 });\nuserMap.set(\"jane\", { age: 30 });\n\nconsole.log(userMap.get(\"john\"));  \u002F\u002F { age: 25 }\nconsole.log(userMap.has(\"alice\")); \u002F\u002F false\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>🔹 Go: Using map\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport \"fmt\"\n\nfunc main() {\n\tusers := map[string]int{\n\t\t\"john\": 25,\n\t\t\"jane\": 30,\n\t}\n\n\tif age, exists := users[\"john\"]; exists {\n\t\tfmt.Println(\"John:\", age)\n\t} else {\n\t\tfmt.Println(\"Not found\")\n\t}\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>สรุป:\u003Cbr>✅ เร็วที่สุด (O(1))\u003Cbr>✅ เหมาะกับระบบที่มีการค้นหาบ่อย เช่น cache, user lookup, หรือ routing\u003Cbr>❌ ใช้หน่วยความจำมากกว่าเล็กน้อย\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>4. สรุปเปรียบเทียบ Searching Algorithms\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cfigure class=\"table\">\u003Ctable>\u003Cthead>\u003Ctr>\u003Cth>วิธีการค้นหา\u003C\u002Fth>\u003Cth>ความซับซ้อน\u003C\u002Fth>\u003Cth>ต้องเรียงข้อมูลก่อน\u003C\u002Fth>\u003Cth>ความเร็ว\u003C\u002Fth>\u003Cth>เหมาะกับ\u003C\u002Fth>\u003C\u002Ftr>\u003C\u002Fthead>\u003Ctbody>\u003Ctr>\u003Ctd>Linear Search\u003C\u002Ftd>\u003Ctd>O(n)\u003C\u002Ftd>\u003Ctd>❌\u003C\u002Ftd>\u003Ctd>ช้า\u003C\u002Ftd>\u003Ctd>ข้อมูลไม่เรียง\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Binary Search\u003C\u002Ftd>\u003Ctd>O(log n)\u003C\u002Ftd>\u003Ctd>✅\u003C\u002Ftd>\u003Ctd>เร็ว\u003C\u002Ftd>\u003Ctd>ข้อมูลเรียงแล้ว\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Map Lookup\u003C\u002Ftd>\u003Ctd>O(1)\u003C\u002Ftd>\u003Ctd>❌\u003C\u002Ftd>\u003Ctd>เร็วมาก\u003C\u002Ftd>\u003Ctd>ระบบที่ต้องค้นหาบ่อย\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003C\u002Ffigure>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>5. แนวทางเลือกใช้ในระบบจริง\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>🧭 Linear Search → ใช้ในกรณีข้อมูลมีขนาดเล็ก หรือไม่เรียงลำดับ\u003Cbr>⚡ Binary Search → ใช้กับข้อมูลที่เรียงแล้ว เช่น การค้นหาหมายเลข หรือสินค้า\u003Cbr>🚀 Map-based Lookup → เหมาะกับระบบที่ต้องค้นหาบ่อย เช่น\u003C\u002Fp>\u003Cul>\u003Cli>Session Management\u003C\u002Fli>\u003Cli>API Caching\u003C\u002Fli>\u003Cli>User Storage\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ตัวอย่างการใช้งานจริง:\u003C\u002Fp>\u003Cul>\u003Cli>ระบบ Login → ใช้ Map-based Lookup (username → user info)\u003C\u002Fli>\u003Cli>ระบบค้นหาแบบ Autocomplete → ใช้ Binary Search\u003C\u002Fli>\u003Cli>ระบบสอนหรือจำลอง Algorithm → ใช้ Linear Search\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Chr>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>สรุป\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>การเลือกใช้ Searching Algorithm ที่เหมาะสมสามารถเพิ่มประสิทธิภาพของระบบได้มหาศาล โดยเฉพาะเมื่อปริมาณข้อมูลมีขนาดใหญ่ขึ้น\u003C\u002Fp>\u003Cul>\u003Cli>💻 JavaScript → เหมาะกับการเรียนรู้ algorithm และการประยุกต์ใช้งานฝั่ง frontend\u003C\u002Fli>\u003Cli>⚙️ Go → เหมาะกับระบบ production-level ที่ต้องการความเร็วและการจัดการหน่วยความจำอย่างมีประสิทธิภาพ\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>ตอนต่อไป\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ใน EP.37 ของซีรีส์ JS2GO เราจะพาคุณไปเรียนรู้เรื่อง Recursion และ Iteration ใน JavaScript และ Go เพื่อเปรียบเทียบแนวทางการเขียนโค้ดแบบ การเรียกซ้ำ (Recursion) และ การวนซ้ำ (Iteration) ว่าภาษาใดทำได้ดีกว่าในแง่ของ Performance และ Readability 🧠\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>","71_11zon_fhhdh9rncf.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fkc2for0fr30jztz\u002F71_11zon_fhhdh9rncf.webp","2026-03-04 08:45:33.253Z","",{"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:20:42.484Z","vslzz9nvv6n77cx","JavaScript","2026-04-10 16:07:28.339Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:45:32.020Z","8vrqzeheyjmdnla","Binary Search","2026-04-10 16:12:59.578Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:45:32.242Z","qrrtdp2now459ki","Linear Search","2026-04-10 16:12:59.642Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:34:57.930Z","3j6u3nyhnmlp6ld","Searching Algorithms","2026-04-10 16:12:34.004Z",{"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":71,"views":72},"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":66,"priority":67,"publish_at":68,"scheduled_at":13,"status":69,"updated":70,"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":66,"th":66},"JS2GO",10,"2025-08-11 03:41:08.820Z","published","2026-04-25 02:32:14.339Z","zwxfag96iy33ciz",213,"kc2for0fr30jztz",[20,25,30,35,40],"2025-11-12 02:36:39.446Z","เรียนรู้เทคนิคการค้นหาข้อมูลอย่างมีประสิทธิภาพผ่าน 3 อัลกอริทึมสำคัญ — Linear Search, Binary Search และ Map-based Lookup","js2go-ep36-searching-algorithms-in-go-and-javascript","2026-04-25 02:47:54.507Z",1,{"th":77}]