Algorithm|Javascript Algorithm

Why Developers Need to Look Beyond Coding Tests

2
Why Developers Need to Look Beyond Coding Tests

Chris is a frontend developer. He used to think like this:

"Algorithms? Isn't that just something you memorize briefly for coding tests? In actual work, lodash or Array.filter handles everything."

One day, Chris was put in charge of a shopping mall admin page. The feature required comparing a list of 50,000 members against 50,000 order records to filter out only "members with order history."

Chris confidently wrote the code.

javascript
// ❌ Chris's "Intuitive" Code
const users = [...Array(50000)].map((_, i) => ({ id: i, name: `User${i}` }));
const orders = [...Array(50000)].map((_, i) => ({ userId: i, product: `Prod${i}` }));

// Iterate through all users and 'search' if their ID exists in the order list.
const activeUsers = users.filter(user => {
  return orders.some(order => order.userId === user.id);
});

"Simple enough!"

The moment he pressed the execute button, the browser froze.

The mouse cursor spun endlessly, and the Chrome tab turned white with the 'Not Responding' message. The user couldn't see the screen even after 5 seconds.

The feature was implemented, but the code failed. The logic Chris wrote performed 50,000 × 50,000 operations—that's 2.5 billion comparisons.

This is the real reason we need to learn algorithms. Algorithms are not just subjects for rote memorization for interviews; they are "procedures to solve problems most efficiently with limited resources (time, memory)."

1. What is an Algorithm?

The word "Algorithm" might sound grand, but it's actually like a 'Cooking Recipe'.

It is a process of taking an input (ingredients), processing it in a set order (recipe), and producing a desired output (dish).

However, even when cooking the same ramen, someone might finish in 3 minutes, while someone else might take 30 minutes because they keep boiling and cooling the water. Programming is the same.

  • Input: 50,000 user records.
  • Output: List of users who ordered.
  • Algorithm (Process): How will we find them?
  • Chris's method (nested loops) was the most inefficient recipe.

    2. Why is this Critical in JavaScript?

    "Computers are fast these days, so does it really matter if the code is a bit rough?"

    For JavaScript developers, there is one fatal constraint. It is Single Threaded.

    JavaScript can only handle one task at a time. If the main thread is busy performing 2.5 billion operations like in Chris's code, the browser cannot handle button clicks, screen updates, or animations. In other words, the UI Freezes.

    Unlike environments that actively use multi-threading like C++ or Java, in the JavaScript environment (Browser, Node.js), inefficient algorithms immediately destroy the User Experience (UX).

    3. CPR for Chris's Code

    Let's apply algorithmic thinking. The core of the problem is that iterating through the orders array from beginning to end every time is too slow.

    JavaScript's Objects or Sets can find data instantly using a Key, like opening a lock, without iterating through loops. (This is called a Hash Table.)

    javascript
    // ✅ Algorithmically Improved Code
    
    // 1. Create a Set of userIds from the order history. (Make search speed O(1))
    // This process runs only 50,000 times.
    const orderUserIds = new Set(orders.map(order => order.userId));
    
    // 2. Iterate through the user list and check if they exist in the Set.
    // Set.has() checks instantly without a loop.
    const activeUsers = users.filter(user => orderUserIds.has(user.id));

    The result?

  • Before: 2.5 billion operations (Took several seconds)
  • After: 100,000 operations (50k Set creation + 50k checks) -> Less than 0.1 seconds
  • We only changed the logic slightly, but performance improved 25,000 times. The browser is snappy, and the user is happy.

    4. What We Will Learn

    In this roadmap, we won't just memorize sorting functions. We will train to understand the characteristics of the JavaScript language and pull out the right tools for the situation.

  • Understanding Data Structures: When should I use an Array, and when should I use an Object/Set?
  • Time Complexity (Big-O): How to predict how much slower my code will get as data increases.
  • Problem Solving Patterns: Efficient patterns created by senior developers, such as sorting, searching, and dynamic programming.
  • Key Takeaways

  • Definition: An algorithm is a sequence of procedures (recipe) to solve a problem.
  • Importance: Since JavaScript is a single-threaded language, inefficient algorithms cause UI Freezing, which is fatal.
  • Effect: Even for the same result, improving the algorithm can boost performance by thousands or tens of thousands of times.
  • Goal: Develop the ability to write code that survives increasing data loads, not just code that "works."

  • Now that you've realized the necessity of algorithms, it's time to inspect your tools.

    Do you know exactly how fast or slow the array methods we use daily—push, pop, shift—are internally?

    Continuing in the next post: "JavaScript Prototype Methods: The Hidden Cost Behind Convenience."

    🔗 References

  • MDN - JavaScript Keyed Collections (Set, Map)
  • VisuAlgo - Visualising data structures and algorithms
  • Comments (0)

    0/1000 characters
    Loading comments...