Tag: Optimization

  • Don’t Forget About JavaScript Iterators

    In Stop turning everything into arrays (and do less work instead) Matt Smith argues that modern JavaScript developers often default to converting data into arrays so they can chain familiar methods like .map(), .filter(), and .slice(). That habit, he says, leads to unnecessary work—extra memory usage, wasted computation, and slower apps.

    Instead, he encourages leaning on iterators and generator functions, which let you process data lazily and sequentially. With iterators:

    • Work happens only when consumed, not upfront.
    • You avoid building full arrays when you only need a slice of the results.
    • You can stream data from APIs or large collections without blowing up memory.
    • You can compose transformations without paying the cost until the final .toArray().

    His rule of thumb: If you don’t need the whole array, don’t create one. Iterators represent “work that hasn’t happened yet,” making them ideal for efficient pipelines, async data fetching, and scenarios where you only need a subset of results.