Iterators, generators, finally, and scarce resources (Was: April 10 2014 Meeting Notes)
C. Scott Ananian
ecmascript at cscott.net
Tue Apr 29 12:11:41 PDT 2014
I'm sympathetic to the "simplicity" argument that says that `.throw` is the
proper way in JS to clean up an iterator. The `.return` proposal seems
like a kludge to work around the fact that ES6 still doesn't have a
discriminatory `catch` clause that can avoid catching the thrown cleanup
exception. But to continue the `throw`-based design you have to provide a
way to throw an exception when exiting the for-of, and I feel like the
result is ultimately inferior, both in performance and aesthetics.
I find Dave's rebuttal convincing. From a performance standpoint, it has
been mentioned that it is *thrown exceptions* which are the performance
cliff, not merely setting up handlers via try. From that perspective,
invoking `.return` on break or exceptional exit from `for-of` seems the
cheapest safe option.
ps. fwiw, I believe that concerns about uptake of `for of` are to some
degree putting the cart before the horse: the usual rule of thumb is that
80% of code is not performance critical, and `for-of` will be used in this
code if it helps code clarity, etc. Predictable clean up of iterators may
well be one of the factors which allow it to increase code
clarity/robustness/etc. If/when developers use it frequently, it will
become a target for optimization.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the es-discuss