Skip to content

Array for/of iteration performance #1392

Description

@bnoordhuis

Iterating over fast arrays currently is 2.5x slower than it should be. Example:

for (const x of [1,2,3]) print(x)

It goes through the whole rigamarole of:

  1. invoking [Symbol.iterator]()
  2. creating an iterator object iter
  3. repeatedly invoking iter.next() until it's exhausted

For fast arrays, all three steps are pure overhead.

A fast array fast path is in principle easy to implement except for the perennial problem of mutation while iterating. Maybe track the array's JSShape and bail out if it changes?

Bailing out can be done by calling js_create_array_iterator() and updating it->idx to the current index.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions