Different ways to create an iterator for values

Asked

Viewed 61 times

3

Is there any difference between creating a iterator for the values of an array using these two forms?

let myIterator = arr[Symbol.iterator]()
let myIterator2 = arr.values()

I’ve run some tests, and at least the results look the same. I wonder if "in the bowels" there is any difference as performance or other relevant characteristics.

1 answer

2

According to the specification of Array.prototype[@@iterator]:

The initial value of the property @@iterator is the same function-object as the initial value of the property Array.prototype.values.

If you found the term "function-object" strange, be sure to refer to this another answer.

Thus, there is no difference between the two ways of executing this method. It is the exact same function, which is shared between these properties. There is also no difference in performance.

However, I believe that using the method values is more preferred in this case, since it is a method created to be used directly by the developer. The @@iterator, however, in addition to being less clear, it was implemented to be used by the language itself, as part of the iteration protocol of language.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.