1

What's the best way to loop through a JsonJavaArray?

JsonJavaArray jjArray = (JsonJavaArray) jsonObject.getAsArray('data');
for(int idx=0;idx<jjArray.size();idx++)

or

for (Iterator<Object> jjObj = factory.iterateArrayValues(jjArray); jjObj.hasNext();)

Does one perform better than the other?

1 Answer 1

2

Does one perform better than the other?

Probably yes.


Does it matter?

Probably no.


Is this "premature optimization"?

Probably yes.


Stop evading the question! Which one is faster?

I would predict that the first form is slightly faster, but if you really want to be sure then you should benchmark both forms using a Java framework like Caliper to make sure that your benchmark is valid.

But a better idea is to pick the variant that your gut feeling says is best, complete and test the program,and then profile it running on real data. If the profiling tells you that this loop is a performance bottleneck, then try the alternative and see if it make any difference.

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.