{"id":1393,"date":"2013-12-05T13:32:28","date_gmt":"2013-12-05T19:32:28","guid":{"rendered":"http:\/\/www.philhassey.com\/blog\/?p=1393"},"modified":"2013-12-05T13:33:48","modified_gmt":"2013-12-05T19:33:48","slug":"lua-on-javascript-comparison-lua-vm-js-lua-js-lua-js-phil-lua5-1-js-native","status":"publish","type":"post","link":"https:\/\/www.philhassey.com\/blog\/2013\/12\/05\/lua-on-javascript-comparison-lua-vm-js-lua-js-lua-js-phil-lua5-1-js-native\/","title":{"rendered":"Lua on Javascript comparison &#8211; lua.vm.js, lua.js, lua.js-phil, lua5.1.js, native"},"content":{"rendered":"<p>So I spent a bit of extra time this week messing with my Lua on Javascript stuff for game jams.  Here&#8217;s the resulting game <a href=\"http:\/\/www.galcon.com\/stuff\/turkey-html5\/\">Turkey Tomahawk Turbo<\/a>.<\/p>\n<p>I checked out the following Lua on Javascript implementations: <a href=\"http:\/\/kripken.github.io\/lua.vm.js\/lua.vm.js.html\">lua.vm.js<\/a>, <a href=\"https:\/\/github.com\/mherkender\/lua.js\">lua.js<\/a>, and <a href=\"https:\/\/github.com\/logiceditor-com\/lua5.1.js\">lua5.1.js<\/a>. I also made a fork of lua.js called <a href=\"https:\/\/github.com\/philhassey\/lua.js\">lua.js-phil<\/a>.<\/p>\n<p>Here&#8217;s the short summary breakdown:<\/p>\n<p>&#8211; lua.vm.js: An asm.js (emscripten) port of Lua 5.2.  It has slickest web presence, but WORST implementation. It&#8217;s crazy buggy and doesn&#8217;t work for anything beyond the cool tech demo. If you really like what they have so far, you&#8217;ll want to spend a week or so fixing their API so it actually works for non-trivial programs.  I never got my game working fully with this due to sporadic runtime errors and other mess.<\/p>\n<p>&#8211; lua.js: A script that converts your Lua script to Javascript.  The official distribution works great, but it is missing some functions so you&#8217;ll may have to modify your program somewhat to work without those functions.  (The string formatting and pattern matching is not implemented.)<\/p>\n<p>&#8211; lua.js-phil: A fork of lua.js.  I added string.format and improved performance by making the following assumptions: you will not use meta-methods (__index, __call) for anything but calling functions \/ methods. You will not use the math meta-methods (__add, &#8230;).<\/p>\n<p>&#8211; lua5.1.js: An asm.js (emscripten) port of Lua 5.1. It has a very nice working API that is exactly like the C API, so if you are used to integrating Lua in C, you&#8217;ll find it pretty easy to integrate with your Javascript program.  It is a build of Lua 5.1, so no missing functions or other quirks.<\/p>\n<p>Here&#8217;s the performance data in FPS.  For comparison, with Lua running on my Mac, I got 400 FPS.<\/p>\n<table border=1>\n<tr>\n<th>\n<th>Chrome 31<\/p>\n<th>Firefox 25<\/p>\n<th>Safari 7<\/p>\n<th>IE 10<\/p>\n<th>Average<\/tr>\n<tr>\n<th>lua.js<\/p>\n<td>51<\/p>\n<td>50<\/p>\n<td>20<\/p>\n<td>110<\/p>\n<td>58<\/tr>\n<tr>\n<th>lua.js-phil<\/p>\n<td>72<\/p>\n<td>59<\/p>\n<td>32<\/p>\n<td>160<\/p>\n<td>81<\/tr>\n<tr>\n<th>lua5.1.js<\/p>\n<td>85<\/p>\n<td>75<\/p>\n<td>4<\/p>\n<td>60<\/p>\n<td>56<\/tr>\n<\/table>\n<p>So to conclude, lua5.1.js would be my preference, except asm.js code takes a horrible performance hit on Safari, and a non-trivial performance hit on IE.  Once all browsers are optimized to work with asm.js code, that would be the best option for sure. Since I was able to modify my game (about 5 lines of code different) to work with those restrictions I mentioned above, I was able to get pretty good performance with my fork of lua.js.  The original lua.js is quite good, short of the missing functions.  <\/p>\n<p>Lastly, there have been many claims of how &#8220;you&#8217;ll get near native performance using javascript, especially now that we have asm.js and emscripten&#8221; and whatnot. Well, I&#8217;m sure in theory that&#8217;s great and all. But no matter how I slice it here, I&#8217;m getting a 4x+ performance hit by running my games on the web.  Still I&#8217;m impressed by what I&#8217;ve seen and I hope more work goes into getting asm.js support in browsers so we can get nearer to native performance!<\/p>\n<p>-Phil<\/p>\n<p>P.S. You can download the whole mess with build scripts and the various Lua things <a href=\"http:\/\/www.galcon.com\/stuff\/turkey-luajs.zip\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So I spent a bit of extra time this week messing with my Lua on Javascript stuff for game jams. Here&#8217;s the resulting game Turkey Tomahawk Turbo. I checked out the following Lua on Javascript implementations: lua.vm.js, lua.js, and lua5.1.js. I also made a fork of lua.js called lua.js-phil. Here&#8217;s the short summary breakdown: &#8211; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-1393","post","type-post","status-publish","format-standard","hentry","category-crazy"],"_links":{"self":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts\/1393","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/comments?post=1393"}],"version-history":[{"count":12,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts\/1393\/revisions"}],"predecessor-version":[{"id":1405,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts\/1393\/revisions\/1405"}],"wp:attachment":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/media?parent=1393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/categories?post=1393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/tags?post=1393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}