4 ms·
What exactly is this trying to test? This headline seems to imply that it's testing the performance of the different sorts of loops in JavaScript, but it's not
by Millennium 13y ago
What exactly is this trying to test? This headline seems to imply that it's testing the performance of the different sorts of loops in JavaScript, but it's not a valid test for that, because the for loop does considerably less work than the do and while loops.
- sebilasse 13y agoHe probably just wanted to test the difference between 'while ' and 'do while' and takes the fastest possible solution (for) as a reference. It's however good to know that 'do while' is that much slower compared to while.
- anonymoushn 13y agowhile is faster than for here. Testing in Chrome 27.0.1453.116 32-bit on Windows Server 2008 R2 / 7 64-bit
- blorenz 13y agoI disagree with you saying this isn't a valid test but added http://jsperf.com/while-vs-for-queue/4 http://jsperf.com/while-vs-for-queue/4 for your convenience.
- sebilasse 13y agoThe for loop uses array access "items[j]", and the while loop uses "items.shift". At the end of the for loop 'items' is still the same, at the end of the while loop 'items' is empty.
- blorenz 13y agoAgreed. The context of this problem was a discussion with a colleague about using this in the order of a queue where the items array would represent transformations to apply to another object. Point taken, though!
- pseudosavant 13y agoThe original test isn't really valid for comparing loops and your tests prove it. The for loop on my machine does 248MM ops/sec and the while (with no mutation) does 243MM, but the while loop with mutation is 0.44 ops/sec. So performance is basically the same when the workload of the loop is the same.