# Reason for different execution order of the mapping fn in \`Array.from\` and \`TypedArray.from\`?

**URL:** <https://es.discourse.group/t/reason-for-different-execution-order-of-the-mapping-fn-in-array-from-and-typedarray-from/1095>\
**Category:** I have questions\
**Created:** [November 17, 2021, 1:25pm UTC](https://es.discourse.group/t/reason-for-different-execution-order-of-the-mapping-fn-in-array-from-and-typedarray-from/1095 "2021-11-17T13:25:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicolo-ribaudo](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nicolo-ribaudo/32/855_2.png) [@nicolo-ribaudo](https://es.discourse.group/u/nicolo-ribaudo)\
**Post date:** [November 17, 2021, 1:25pm UTC](https://es.discourse.group/t/reason-for-different-execution-order-of-the-mapping-fn-in-array-from-and-typedarray-from/1095/1 "2021-11-17T13:25:32Z")

</div>

I was reading the `%TypedArray%.from` spec and I noticed that it works differently from `Array.from`: it first fully iterates its iterable argument, and then it maps every value.

The difference can be observed with this code:

```javascript
function* generate() {
  for (let i=0;i<3;i++) {
    console.log(`Generate ${i}`);
    yield i;
  }
}

function map(v) {
  console.log(`Map ${v}`);
  return v;
}

console.log("Array.from");
Array.from(generate(), map);

console.log("%TypedArray%.from");
Uint8Array.from(generate(), map);

```

It will produce this output:

```javascript
Array.from
Generate 0
Map 0
Generate 1
Map 1
Generate 2
Map 2

%TypedArray%.from
Generate 0
Generate 1
Generate 2
Map 0
Map 1
Map 2

```

I tried looking at older versions of the spec to see why they work differently, but this difference was already in the July 15, 2013 ES6 draft (which is the draft introducing `%TypedArray%.from` and iterables support in `Array.from`).

Does anyone know what is the reason behind this difference, or if there isn't a reason?

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [November 17, 2021, 3:38pm UTC](https://es.discourse.group/t/reason-for-different-execution-order-of-the-mapping-fn-in-array-from-and-typedarray-from/1095/2 "2021-11-17T15:38:36Z")

</div>

I brought this up in committee:  
[https://github.com/tc39/notes/blob/8711614630f631cb51dfb803caa087bedfc051a3/meetings/2019-07/july-23.md#inconsistency-between-arrayfrom-and-typedarrayfrom](https://github.com/tc39/notes/blob/8711614630f631cb51dfb803caa087bedfc051a3/meetings/2019-07/july-23.md#inconsistency-between-arrayfrom-and-typedarrayfrom)

As I recall, there wasn’t a strong reason for it, and it “should” have been addressed as part of ES6, but absent a compelling performance story to change it, nobody had any interest in doing so.

---

<div class="post-metadata">

**Author:** ![nicolo-ribaudo](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nicolo-ribaudo/32/855_2.png) [@nicolo-ribaudo](https://es.discourse.group/u/nicolo-ribaudo)\
**Post date:** [November 17, 2021, 3:41pm UTC](https://es.discourse.group/t/reason-for-different-execution-order-of-the-mapping-fn-in-array-from-and-typedarray-from/1095/3 "2021-11-17T15:41:45Z")

</div>

Thanks for the link!
