What is the difference between the two array structures and their repetition loops in Javascript?

Asked

Viewed 173 times

6

I would like to know which is the best way to work with arrays?

var meu_array = new Array(1, 2, 3);
var meu_array = [1, 2, 3];

There is some difference in performance between the 6 cases presented in the use of loop below?

 // foreach 
    meu_array.forEach(function(valor, i){
        console.log(valor, i);
    }); 
 // for key associativa
    for (var i in meu_array) {
        console.log(meu_array[i], i);
    }
 // for valor associativo  
   var i = 0;
   for (value of meu_array) {
       console.log(value, i++);
   }
 // for interator
    for (var i=0; i<=meu_array.length; i++) {
        console.log(meu_array[i], i);
    }
 // while
    var i=0;
    while (i <= meu_array.length) {
          console.log(meu_array[i], i);
     i++;
    }
 // jquery
    $.each(meu_array, function(i, value) {
      console.log(value, i);
    });
  • 1

    If you want to know about the performance of each algorithm, ask another question, don’t ask two questions in one. And what is the tag Acme?

  • I’m also curious about Acme. I think he wanted to write Ecmascript. rs

  • rs...typed wrong.

  • Are you going to ask the other question? This one is very weird, array question, then it talks about loops, they’re separate things, separate them. I answered about the array because that’s what’s in the title. If you want to know about the loops, ask the question about them. If you do not want to, just remove this part of the question so as not to get confused. If you will, I will already prepare the answer.

  • I believe everything can be answered in a single question.

  • No, you cannot, they are two completely dissociated things. Nor do you believe since I did not answer the second part and you accepted my answer. If you will insist on keeping this as two questions in one I will vote to close as very wide. What will you do?

Show 1 more comment

1 answer

5


Essentially it makes no practical difference. They create the same array and the performance will be the same for him. The performance may be a little better for the first case, but only in creation and it will be very little difference to care about. And this depends on language implementation. Don’t count on this.

There is a difference in performance by the algorithm used in each exemplified situation, but not because the array was created one way or another.

The first way is considered confusing, because if you want to create a array with only one element, it will not do what you expect. It will create a array with the specified amount of elements and not a array with an element of that value.

In addition she will call the builder who will build the array. The second way will create the array without going through the constructor. This may seem to give in it. But there are no guarantees in the language that this is true. It is possible to override the method.

The general recommendation is not to use the Array() unless you really need it and you know what you’re doing and why you’re choosing it.

  • I agree with you, in case you avoid using Array object instance, in fact I prefer to avoid the general use of array, like more to work with object.

  • I don’t know if it makes a difference, the array is a disguised object.

  • actually the type of object that should be array, is an object... javascript is very messy.

  • Check it out: https://www.youtube.com/watch?v=FqhZZNUyVFM

  • 4

    These things are crude and lead nowhere. They are fans of one language speaking badly of another language. The reverse is also always possible. Everything exists for a reason.

Browser other questions tagged

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