3

I'm trying to understand how the get item at index works in an array in JavaScript.

For example imagine I have a for iterating an array with thousands (or millions) of items. Which would be the best way (with the best performance) to print the items?

// Option 1
for (int i = 0; i < array.length; i++) {
    var data = array[i];
    console.log( data.ID, data.name, data.date, data.description );
}

// Option 2
for (int i = 0; i < array.length; i++) {
    console.log( array[i].ID, array[i].name, array[i].date, array[i].description );
}

Is it option 1 or option 2? Or is it the same thing to use the first or second option? Or is there an even better way to do this?

I know for instance that get() in the Java ArrayList works this way, so obviously the best approach would be option 1, since it checks the range each time it calls the get() method.

public E get(int index) {
    rangeCheck(index);

    return elementData(index);
}

private void rangeCheck(int index) {
    if (index >= size)
        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}

Does JavaScript works in a similar way?

The "big question" here is really how does JavaScript array get item at index works

Note: I've tried to search for the best way to get an element in an array, but all I could find was the best way to iterate an Array. That isn't what I'm trying to get from this question!

3
  • This depends on what do you mean by "print". If it means console.log, then the best way is apply console.log at whole array: console.log(array), or console.log(jSON.stringify(array)) Commented Sep 18, 2015 at 13:24
  • 1
    Since there are a few engines that execute JS, your question can't be answered entirely. The answer is that it's absolutely irrelevant. However, to conclude in real-world scenario which is better - why don't you measure it yourself? Just take 3 most popular browsers and profile what executes the fastest. I think that you shouldn't even waste your time on this optimization, it's absolutely pointless. Commented Sep 18, 2015 at 13:27
  • The way to test would be to create a timer, try both with dynamically generated arrays, and console.log out the resulting times. Commented Sep 18, 2015 at 13:28

1 Answer 1

3

EDIT

Forget everything I've said. It was true for very old browsers. For the new ones (tested with Chrome, Firefox, IE11 and Edge), there is almost no difference between any of those examples.

I've tested with an array of 100 million records, and the difference is < 1ms.

The Option 2 apparently is slightly faster, nothing that will really count.

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

3 Comments

Browsers optimize for loops over arrays. You likely won't find any performance difference caching the length.
Your second example will stop the loop when the element is false or 0 or an empty string.
@torazaburo Sure, it will stop with any falsy values. That's why I've said that he must ensure his objects are not null / undefined (since he's working with objects, not strings, or booleans, or numbers.

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.