Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript ES6 yield vs yield in python [closed]

I have been working on Python and JS for some time and wanted understand the difference in behavior between the yield for generators in python vs in ES6

Using yield in python saves memory as it generates 1 item at a time . Is this the same for the yield in ES6?

like image 428
Praveen Avatar asked Sep 26 '22 11:09

Praveen


1 Answers

The short answer is "Yes."

The simplified long answer is "Yes, though generators have some overhead (fixed in the sense that it doesn't change with the number of items to be generated) that make the savings vanish when the number of items to be generated is small." (but of course, in that case, the total cost is small enough that it rarely matters anyway)

Excessively complex caveat to the long answer:

CPython (the reference Python interpreter) does very little optimization of code; it can make small peephole optimizations in very limited circumstances (e.g. it can convert 1 + 2 + x to 3 + x in the byte code, but due to operator overloading and order of operations, it can't convert x + 1 + 2 to x + 3, because it can't assume x + 1 will return an int, and it can't know that addition is associative for whatever type x turns out to be). So when you use a generator in CPython, it will always be executed as a generator at run time.

By contrast, on most modern browsers, the JavaScript engine uses JIT-ing to compile the JavaScript code down to native code; it can make speculative/adaptive optimizations where it compiles to code that assumes specific types and values and falls back on interpreting the original JS only if the assumptions fail. Which means you can't actually say for sure what will be done in the case where your generator code is executed in a hot loop (where expensive analysis and optimization would be considered worthwhile for the long term savings).

If the JS engine determines that the generator is usually producing a small number of outputs that can be calculated ahead of time, that are usually fully consumed, that the generation process has no visible side-effects, and that it would be more efficient and not too memory intensive, it would be well within its rights to produce native code that doesn't actually create or run a generator, but instead produces an Array of outputs (or for stuff like numbers, perhaps an ES6 typed array to reduce memory usage). I have no insight into which JS engines, if any, might attempt to perform such an optimization, but given how rapidly state of the art has been changing for JS engines over the last seven years or so (the slowest commonly used modern JS engine is probably at least 10x faster than the fastest engine from Sept. 1, 2008, before V8 released), the optimizations that occur today could easily change tomorrow.

like image 103
ShadowRanger Avatar answered Oct 11 '22 10:10

ShadowRanger