http://jsperf.com/jquery-each-vs-for-loop/108
for (var b = a[0], len = a.length; len; b = a[--len]) {
newArray.push(
b
);
}
and
for (var i = 0, len = a.length; i < len; i++) {
newArray.push(
a[i]
);
}
- According to jsref, it says the first one is faster. why?
- Can anyone explain me the for loop on whats its doing compared to traditional way?
Your first example just does something very different. Check it out:
Your second example results in the expected
[1, 2, 3, 4].If you need to understand the algorithms, it might be easier when converting the
for-notation to while-loops and to expand the decrement and increment operators: