LibJS+LibWeb: Add fast path for builtin iterators in iterator_step()

We already have fast path for built-in iterators that skips `next()`
lookup and iteration result object allocation applied for `for..of` and
`for..in` loops. This change extends it to `iterator_step()` to cover
`Array.from()`, `[...arr]` and many other cases.

Makes following function go 2.35x faster on my computer:
```js
(function f() {
  let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  for (let i = 0; i < 1000000; i++) {
    let [a, ...rest] = arr;
  }
})();
```
This commit is contained in:
Aliaksandr Kalenik 2025-05-12 00:31:51 +03:00 committed by Alexander Kalenik
parent 31301ef08b
commit bb53485dea
Notes: github-actions[bot] 2025-05-13 12:15:28 +00:00
6 changed files with 38 additions and 18 deletions

View file

@ -89,11 +89,11 @@ static JS::ThrowCompletionOr<Vector<String>> convert_value_to_sequence_of_string
auto next = TRY(JS::iterator_step(vm, iterator));
// 2. If next is false, then return an IDL sequence value of type sequence<T> of length i, where the value of the element at index j is Sj.
if (!next)
if (!next.has<JS::IterationResult>())
return sequence_of_strings;
// 3. Let nextItem be ? IteratorValue(next).
auto next_item = TRY(JS::iterator_value(vm, *next));
auto next_item = TRY(next.get<JS::IterationResult>().value);
// 4. Initialize Si to the result of converting nextItem to an IDL value of type T.