4 ms·
Just for fun added test case for reduce and forEach, in order to show how slow it is. http://jsperf.com/while-vs-for-queue/3 http://jsperf.com/while-vs-for-queu
by vittore 13y ago
Just for fun added test case for reduce and forEach, in order to show how slow it is. http://jsperf.com/while-vs-for-queue/3 http://jsperf.com/while-vs-for-queue/3
- blorenz 13y agoThanks for the additions. I particularly like the forEach addition because it 'reads' better but is very unperforming. I can foresee this being unknowingly abused.
- Cthulhu_ 13y agoOne thing to note though is that forEach and reduce can both be /theoretically/ faster; for and while loops both imply an order in processing the list, whilst forEach and definitely reduce don't; in theory, the JS interpreter can take the array, split it up into chunks (one for each core / thread), and process the list in parallel, which would / could produce results faster. But, atm, functional operations on lists are still executed single-threaded. Wouldn't mind seeing a WebWorker implementation, for example. Although somehow I think the overhead for a relatively simple calculation like this would negate the results.
- ajanuary 13y agoInteresting that reduce and forEach have about the same performance, suggesting the overhead is in the function invocation, no matter whether it needs to close over variables or not. I would have thought not closing over variables would be substantially quicker.