View : 205

25/04/2026 02:47am

JS2GO EP.36 Implementing Searching Algorithms in Go and JavaScript

JS2GO EP.36 Implementing Searching Algorithms in Go and JavaScript

#JavaScript

#Go

#Binary Search

#Linear Search

#Searching Algorithms

Searching is one of the most fundamental processes in every software system — whether it’s:

  • Searching for users in a database
  • Finding products in an online store
  • Or locating words within a document

 

A well-designed search algorithm can make your system respond faster and utilize resources more efficiently.

 

In this article, we’ll compare Searching Algorithms between JavaScript and Go, focusing on three main techniques:

1️⃣ Linear Search
2️⃣ Binary Search
3️⃣ Map-based Lookup

 

Along with practical insights on when to use each approach in real-world systems 🚀

 

1. Linear Search — Sequential Search

 

Concept:
Iterate through each element one by one until the target value is found (or the list ends).

 

🔹 JavaScript: Linear Search

function linearSearch(arr, target) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === target) return i;
  }
  return -1;
}

const nums = [10, 25, 30, 45, 60];
console.log(linearSearch(nums, 30));  // Output: 2
console.log(linearSearch(nums, 99));  // Output: -1

 

🔹 Go: Linear Search

package main

import "fmt"

func linearSearch(arr []int, target int) int {
	for i, v := range arr {
		if v == target {
			return i
		}
	}
	return -1
}

func main() {
	nums := []int{10, 25, 30, 45, 60}
	fmt.Println(linearSearch(nums, 30)) // Output: 2
	fmt.Println(linearSearch(nums, 99)) // Output: -1
}

 

Summary:
✅ Simple and works for any dataset
❌ Inefficient for large datasets
Time Complexity: O(n)

 

2. Binary Search — Divide and Conquer

 

Concept:
Works only on sorted data by dividing the search range in half with each iteration, drastically reducing search time.

 

🔹 JavaScript: Binary Search

function binarySearch(arr, target) {
  let left = 0;
  let right = arr.length - 1;

  while (left <= right) {
    const mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) return mid;
    if (arr[mid] < target) left = mid + 1;
    else right = mid - 1;
  }

  return -1;
}

const sorted = [10, 25, 30, 45, 60];
console.log(binarySearch(sorted, 45)); // Output: 3

 

🔹 Go: Binary Search

package main

import "fmt"

func binarySearch(arr []int, target int) int {
	left, right := 0, len(arr)-1
	for left <= right {
		mid := (left + right) / 2
		if arr[mid] == target {
			return mid
		}
		if arr[mid] < target {
			left = mid + 1
		} else {
			right = mid - 1
		}
	}
	return -1
}

func main() {
	sorted := []int{10, 25, 30, 45, 60}
	fmt.Println(binarySearch(sorted, 45)) // Output: 3
}

 

Summary:
✅ Much faster for sorted data
❌ Not applicable for unsorted lists
Time Complexity: O(log n)

 

3. Map-based Lookup — Key-Value Search

 

Concept:
Instead of searching linearly, data is stored as key-value pairs, allowing direct access to values in constant time.

 

🔹 JavaScript: Using Map

const userMap = new Map();
userMap.set("john", { age: 25 });
userMap.set("jane", { age: 30 });

console.log(userMap.get("john"));  // { age: 25 }
console.log(userMap.has("alice")); // false

 

🔹 Go: Using map

package main

import "fmt"

func main() {
	users := map[string]int{
		"john": 25,
		"jane": 30,
	}

	if age, exists := users["john"]; exists {
		fmt.Println("John:", age)
	} else {
		fmt.Println("Not found")
	}
}

 

Summary:
✅ Fastest search method (O(1))
✅ Ideal for frequent lookups — such as caching, routing, or user data retrieval
❌ Consumes slightly more memory

 

4. Searching Algorithm Comparison

 

Search MethodTime ComplexityRequires Sorted DataSpeedBest For
Linear SearchO(n)❌ NoSlowSmall / Unsorted Data
Binary SearchO(log n)✅ YesFastSorted Data
Map LookupO(1)❌ NoVery FastFrequent Lookups / Key-Value Data

 

5. Best Practices for Real Systems

 

🧭 Linear Search → Best for small datasets or when the data is unsorted.
⚡ Binary Search → Perfect for sorted datasets like numeric ranges or product lists.


🚀 Map-based Lookup → Best for frequent searches such as:

  • Session Management
  • API Caching
  • User or Configuration Data

 

Examples:

  • Login System → Map-based Lookup (username → user info)
  • Autocomplete System → Binary Search
  • Educational / Demo Systems → Linear Search

 


 

Conclusion

 

Choosing the right search algorithm can greatly improve your system’s performance — especially when working with large-scale datasets.

 

💻 JavaScript → Great for experimenting and implementing algorithms on the frontend.
⚙️ Go → Ideal for production-level systems requiring high performance and efficient memory management.

 

Next Episode

 

In EP.37 of the JS2GO series,
we’ll dive into Recursion and Iteration in JavaScript and Go,
comparing how both languages handle looping vs self-calling functions — and which one performs better in terms of speed and readability 🧠

 

Read more

🔵 Facebook: Superdev Academy

🔴 YouTube: Superdev Academy

📸 Instagram: Superdev Academy

🎬 TikTok: https://www.tiktok.com/@superdevacademy?lang=th-TH

🌐 Website: https://www.superdevacademy.com/en