# Any shortcut to 1:length(myVector)?

**URL:** <https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816>\
**Category:** General Usage\
**Created:** [January 4, 2021, 11:14am UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816 "2021-01-04T11:14:56Z")\
**Posts on this page:** 1\
**Showing post:** 30

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [January 8, 2021, 7:19pm UTC](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/30 "2021-01-08T19:19:08Z")

</div>

If your `container` is a `Vector` and it is indexed a single time, then there will probably be no difference in performance. Sometimes even if it is indexed multiple times (if the compiler can prove it does not change).

I would go with `pairs` in this case anyway because I believe the code may change and `eachindex` may become a worse decision, but I do not see the opposite happening (i.e., I do not see `eachindex` becoming a better decision than `pairs` in a code that use both index and value). But if the code is guaranteed to not change too much (i.e., will keep using `Vector`s and not making so many accesses) then the decision ends up being mostly stylistic. I would only recommend `eachindex` if you really is **semantically** iterating positions and may change the value in the indexed position inside the loop before accessing it again and therefore using `pairs` can lead to a subtle bug of using the old value stored in `value` instead of the new in the indexed position.

EDIT: updated my first answer to address some cases I thought only after after this question.

---

_[View the full topic](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816)._
