M
Manish Tomar
Hi,
I know that using for in loop to iterate over array elements is a bad
idea but I have a situation where it is tempting me to use it. I have
a two dimensional sparse array say "arr" which will have length to be
some 50 but will have some elements like arr[1], arr[10], arr[23] at
random locations. Each of these elements is again an array (since it
is two dimensional) say arr[1][30], arr[1][24] & so on for arr[10 &
23] also. Currently I've coded it like this:
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr.length; j++) {
if (arr && arr[j]) {
// do stuff with arr[j]
}
}
}
but i am tempted to do following
for (var i in arr) {
if (!arr.hasOwnProperty(i)) {
continue;
}
for (var j in arr) {
if (arr.hasOwnProperty(j)) {
// do stuff with arr[j]
}
}
}
thinking it to be faster. What are your views?
Thanks,
Manish
(http://manishtomar.blogspot.com)
I know that using for in loop to iterate over array elements is a bad
idea but I have a situation where it is tempting me to use it. I have
a two dimensional sparse array say "arr" which will have length to be
some 50 but will have some elements like arr[1], arr[10], arr[23] at
random locations. Each of these elements is again an array (since it
is two dimensional) say arr[1][30], arr[1][24] & so on for arr[10 &
23] also. Currently I've coded it like this:
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr.length; j++) {
if (arr && arr[j]) {
// do stuff with arr[j]
}
}
}
but i am tempted to do following
for (var i in arr) {
if (!arr.hasOwnProperty(i)) {
continue;
}
for (var j in arr) {
if (arr.hasOwnProperty(j)) {
// do stuff with arr[j]
}
}
}
thinking it to be faster. What are your views?
Thanks,
Manish
(http://manishtomar.blogspot.com)