2

Why cant I use this approach in React-Native for a faster looping:

var array = this.myinputfields.textfields.length;
for (let i = 0; i < array; i++)

I know this can be done in JavaScript but not in React-Native. Is there a way to loop faster through an array?

5
  • What is wrong with the approach? Any error message? Commented Jun 13, 2019 at 13:21
  • @BreakBB No errors, as I have mentioned I am just asking if there is another approach for a faster performance. Commented Jun 13, 2019 at 13:25
  • @BreakBB the one that I have posted doesnt work in React-Native, it throw error. Commented Jun 13, 2019 at 13:26
  • 1
    Can you describe the error ? Did you have any message ? Commented Jun 13, 2019 at 13:27
  • @R3tep it seems like I was wrong, it actually works. I tried it some days ago but it didnt, maybe there was another problem. However, what's your opinion, do you think this is a faster approach? Commented Jun 13, 2019 at 13:32

1 Answer 1

1

You can use this faster way

for (let l = this.myinputfields.textfields.length; l--;) {}

But you loop on the array in reverse

Sign up to request clarification or add additional context in comments.

4 Comments

can you explain why this is a faster way?
If you read the line, you can see there is one variable less and one operation less. There is only one incrementation and no comparison with <
When should I use this and when the other approach?
You can use this way if it's possible to loop on the array in reverse mode. The other one if it's not possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.