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);
});
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?
– Maniero
I’m also curious about Acme. I think he wanted to write Ecmascript. rs
– Geison Santos
rs...typed wrong.
– Ivan Ferrer
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.
– Maniero
I believe everything can be answered in a single question.
– Ivan Ferrer
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?
– Maniero