Monday, December 2, 2013

JavaScript Yield


Firefox has had support for the yield keyword for a long time already, but Google Chrome is only just recently supporting this features as an experimental option. Basically, this means if you want your website to work in most web browsers, you can forget about using yield and the beauty of generator functions for the next couple years, it is going to take awhile before the majority of users upgrade. It is time to give up on yield? There is one option...

PythonJS now supports generator functions that will work in all browsers by translating the generator into a class with state-machine at compile time. Using PythonJS to write generator functions also produces JavaScript that runs faster than hand-written JavaScript that uses the native yield keyword. This is likely because the native yield is still a rarely used feature, and JIT's have not tuned their performance for it. PythonJS translates generator functions into simple classes with a next method, which is very JIT friendly. The results below show native yield is more than 10 times slower than yield in PythonJS. I was unable to test native yield in Chrome, for some reason I could not get it to work even after switching on harmony.

The Fibonacci series computed 1,000 times to 1,000 places in Firefox28, Python2.7, PyPy2.2, and GoogleChrome. Lower times are better. The code used in this benchmark is below:

No comments:

Post a Comment