Skip to content
Go back

Iterator vs Generator — What’s the Difference?

Published:  at  06:30 AM

Iterators and generators are closely related and often used together, they are not the same. Let’s break it down clearly.

🔁 Iterator

An iterator is an object that defines a sequence and potentially a return value upon completion. It must implement a next() method that returns an object with value and done properties.

Use iterators when:

Example

function createIterator(arr) {
  let index = 0;
  return {
    next: function () {
      if (index < arr.length) {
        return { value: arr[index++], done: false };
      } else {
        return { done: true };
      }
    }
  };
}

const it = createIterator([10, 20, 30]);
console.log(it.next()); // { value: 10, done: false }
console.log(it.next()); // { value: 20, done: false }
console.log(it.next()); // { value: 30, done: false }
console.log(it.next()); // { done: true }

⚙️ Generator

A generator is a special function using function* syntax that can pause and resume execution using the yield keyword.

Use generators when:

Example

function* numberGenerator() {
  yield 10;
  yield 20;
  yield 30;
}

const gen = numberGenerator();
console.log(gen.next()); // { value: 10, done: false }
console.log(gen.next()); // { value: 20, done: false }
console.log(gen.next()); // { value: 30, done: false }
console.log(gen.next()); // { value: undefined, done: true }

🆚 Key Differences

FeatureIteratorGenerator
SyntaxManual object with next()Uses function* and yield
Code ComplexityMore verboseConcise and readable
State ManagementManualAutomatically managed
ReusabilityCustom setup neededCan be reused easily
Use in LoopsHarder, manual loopWorks with for...of out of the box
Lazy EvaluationYes, but manuallyYes, naturally supported
Pause/Resume logicNoYes, with yield
Async SupportNo (without Promises)Yes, via async function*

Examples

🐍 Python Example

Iterator

class MyIterator:
    def __init__(self, limit):
        self.limit = limit
        self.current = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self.current >= self.limit:
            raise StopIteration
        val = self.current
        self.current += 1
        return val

it = MyIterator(3)
for val in it:
    print(val)  # 0 1 2

Generator

def my_generator(limit):
    for i in range(limit):
        yield i

for val in my_generator(3):
    print(val)  # 0 1 2

🌐 JavaScript Example

Iterator (Manual)

const myIterator = {
  current: 0,
  limit: 3,
  [Symbol.iterator]() {
    return {
      current: this.current,
      limit: this.limit,
      next() {
        if (this.current < this.limit) {
          return { value: this.current++, done: false };
        }
        return { done: true };
      }
    };
  }
};

for (const item of myIterator) {
  console.log(item);
}

Generator

function* myGenerator(limit) {
  for (let i = 0; i < limit; i++) {
    yield i;
  }
}

for (const val of myGenerator(3)) {
  console.log(val); // 0 1 2
}

You can also have async generator but you need to await the iterator.

async function* myGenerator(limit) {
  for (let i = 0; i < limit; i++) {
    yield i;
  }
}

for await (const val of myGenerator(3)) {
  console.log(val); // 0 1 2
}

✅ Summary


Suggest Changes

Previous Post
What kind of database should you choose for your startup app?
Next Post
How Generators improves performance ?