For example, I use JavaScript and most of the time I use forEach as following :
arr.forEach(function(item){
//do something here
});
Historically the forEach-loop in JavaScript has been significant slower than for-loop. I don't know if this still applies, but I never use it in JavaScript for that reason.
In PHP I most often use foreach() as it gives me a nice clean code without all the [array brackets], etc.
I use for loop in python:
for item in items:
#do something
In ruby I use a lot the each loop.
items.each |item| do
#stuff
end
It is very clean
At times, I use enhanced for-loop in Java, but there are times where I have to use i++, because it makes lot of sense. Ex. based on position, do something kinda stuff, will be better with i++ because I don't have to introduce another variable!
Yep. Me too a big fan of Foreach loop. PHP, Python and JS, the most helpful one, and the one which aligns best to the use case is foreach.
Best part is foreach loops makes the code more readable and logical.
arr.forEach() looks more logical than for(i=0; i<arr.length;i++)
For me it depends on the task; generally I try to avoid anything that introduces the overhead of function calls on each iteration; a problem with JS Object.forEach and callbacks is it introduces overhead and by definition should be significantly slower on simpler tasks than a simple for loop.
But the question is WHY am I looping and WHAT am I processing. Each of the methods exists for different purposes. I'm soften surprised that you don't see do/while more, though you'll see while/do all the time. MAYBE it's because my first high level language was pascal with it's repeat/until structure, maybe it's because my first programming language was assembly where you load the count and only do logic AFTER an iteration, but the repeat/until (pascal) aka do/while (c, php, js, every other C syntax language) structure is the one that just makes more sense in my head.
Certainly far more sense than setting up a new stack reference, copying values or a pointer onto the stack, and making a far call -- which is what the object based 'each' is basically having to do under the hood to function.
Though with JavaScript arrays being so painfully and agonizingly slow (since they're pointered lists not true arrays of fixed size) it likely makes little difference in that environment.
STILL, most benchmarks and engines for JavaScript will STILL tell you that
for (var i = 0, iLen = a.length; i < iLen; i++) {
is going to be WAY faster than Array.each, though how much faster would depend entirely on what you are doing inside the loop.
Also in JS I still have to support legacy UA's where Array.each and Object.forEach simply don't exist yet, and rather than polyfills I just use classical "for".
I mean hell, look at the code I was just reviewing to see if I could squeeze a hair more performance from it:
; procedure stickUpdate;
pProcNoArgs stickUpdate
mov dx, 0x201
mov cx, stickLimit
xor ax, ax
mov bx, ax
mov di, ax
mov si, ax
mov ah, [stickMask]
push bp
mov bp, bx
cli
.loop:
in al, dx
and al, ah
ror al, 1
adc bx, 0
ror al, 1
adc di, 0
ror al, 1
adc si, 0
ror al, 1
adc bp, 0
or al, al
loopnz .loop
sti
mov [stickData], bx
mov [stickData + 2], di
mov [stickData + 4], si
mov [stickData + 6], bp
pop bp
retf
So... basically the right tool for the right job. Answer varies depending on which language, what's being processed, and even what the target runtime environment is. You can do a lot of stuff in node.js thanks to it being a standalone of V8 you can't do on a website using the same language, since Christmas only knows how much of ECMAScript 262-5, much less -6 is even available to you.
(defn demo-loop [x]
(if (> x 2)
(recur (- x 1)))
1)
(demo-loop 10)
I use in the most time.
// Array
arr.forEach((item) => {
//do something here
});
// Object
Object.keys(object).forEach((key) => {
const item = object[key];
});
But I like analyze the objective of the iterate. If the objective is not read the list I prefer use functional approach's.
In PHP I prefer to make use of the foreach-loop over the alternatives:
foreach ($items as $item) {
// code
}
In Javascript I tend to make use of forEach most of the time:
items.forEach((item) => {
// code
})
In Java I prefer the enhanced for-loop over the alternatives:
for (Int item : items) {
// code
}
i++ is probably the one I use the most.
resultCount = 10;
for( i = 0; i < resultCount; i++ ) {// do something};
with the help of underscore & ES6:
_.each(items, ( item ) => {
// do something
});
Gergely Polonkai
You have to believe in things that are not true. How else would they become?
I wonder if Pythons list comprehensions count:
[e.field for e in a_list if condition]My code base is full of constructs like this.