3 ms·
Really? You find the above loop more readable than: if(array.length > 0) doFunction(array[0]); array.slice(1).forEach(doOtherFunction);
by inglor 13y ago
Really? You find the above loop more readable than:
if(array.length > 0) doFunction(array[0]);
array.slice(1).forEach(doOtherFunction);
- goldenkey 13y agoYou're kind of cheating because you're using slice which will create a copy of the Array, and that's not cheap if the array is large.
- inglor 13y agoPeople use arrays for things like queues and stacks all the time in JavaScript - they're _much_ slower than hand rolled collection (eg. http://jsperf.com/deque-vs-array-2 http://jsperf.com/deque-vs-array-2 ) - why not micro optimize that as well? If you have a very large array of course optimization can/should be considered but that's simply not the average case.
- goldenkey 13y agoI actually like the slice code for it's ease of readability. Functional methods are extremely nice and I hope they get faster and faster with the optimization of JS JITs. I never heard of Dequeue [1], that is mighty cool. I agree that premature optimization is not good - but when it can be done cleanly without hurt to the readability of the code, I'd say to keep it in ones' repertoire, to effectively write more performant code, more often, on average. [1] http://code.stephenmorley.org/javascript/queues/ http://code.stephenmorley.org/javascript/queues/