Skip to content
Go back

How Generators improves performance ?

Published:  at  06:30 AM

In modern software systems, we often work with large datasets β€” think server logs, customer records, telemetry streams, or product catalogs. Loading everything into memory just to loop over it can easily bring your system to its knees.

The smarter way? Use iterators or generators that process data one piece at a time.


🌍 Real-World Use Case: Processing a Large Log File

Imagine you’re analyzing a log file with 1 million entries β€” stored line-by-line. You want to extract specific events or count errors. Let’s see how to do this efficiently in Python and JavaScript.


❌ Memory-Heavy: Read Entire File in Memory

const fs = require("fs");

const lines = fs.readFileSync("server.log", "utf-8").split("\n");

for (const line of lines) {
  if (line.includes("ERROR")) {
    console.log("Found an error:", line);
  }
}

This loads the entire file into memory, which may be fine for small logs β€” but dangerous for huge files.

βœ… Memory-Efficient: Async Generator

const fs = require("fs");
const readline = require("readline");

async function* readLogLines(filePath) {
  const fileStream = fs.createReadStream(filePath);
  const rl = readline.createInterface({ input: fileStream });

  for await (const line of rl) {
    yield line; // yield one line at a time
  }
}

// Process the log
(async () => {
  for await (const line of readLogLines("server.log")) {
    if (line.includes("ERROR")) {
      console.log("Found an error:", line);
    }
  }
})();

While this reads line one after another, making it efficient.

In Python,

def read_log_lines(file_path):
    with open(file_path, 'r') as file:
        for line in file:
            yield line.strip()  # Yield one line at a time

# Process the log
for line in read_log_lines("server.log"):
    if "ERROR" in line:
        print("Found an error:", line)

βœ… Key Takeaways

TechniqueMemory UsageSuitable For
Full LoadHighSmall datasets
Generator/StreamLowLarge files, real-time streams, infinite data

🧠 Summary


πŸ“Œ Pro Tip: Always ask: Do I really need the entire dataset in memory? If not, generators might be your best friend.


Suggest Changes

Previous Post
Iterator vs Generator β€” What’s the Difference?
Next Post
Understanding the Relation Between FastAPI and Uvicorn