Wednesday, March 30, 2011

JavaScript vs Lua


  I've used Lua  in the past, mostly as a scripting language embedded into games, but lately I've been doing some web programming which requires the use of JavaScript.

 The two languages are similar in their approach to OOP, which is prototypes not classes.  This takes some getting used to, if coming from a traditional class based language, but does offer some nice advantages.

 But I've started to notice aspects of JavaScript which seem glaringly inept when compared to Lua.


Advantage Lua:
1)  Lua "tables" are essentially equivalent to JavaScript's "object", yet tables are vastly superior as they offer both the functionality of a hash map and that of an array.  Additionally Lua tables can be keyed by any type, be it string, number or even other tables.  JavaScript is far more limited, the only thing that you can key "objects" by is a string.
  JavaScript offers a separate type called Array which allows you to do what you can already do in Lua with it's basic table.

2)  JavaScript has no functionality for importing code from one file into another.  Most people seem to resort to either creating a mega file, which contains all of their code, or just importing every file at the top of their HTML page.  This seems exceedingly crude to me.

3) LuaJIT is a custom virtual machine which gives Lua the best performance of any Dynamic language, even Google's V8 doesn't compete(over 2X as slow).  Huge advantage to Lua for this.


Advantage JavaScript:


 1) Now syntax wise I do prefer JavaScript, for as long as I used Lua I just never felt any particular love for the Fortran style of syntax, which requires excessive typing   JavaScript uses a syntax very similar to C, C++ etc. so most programmers will immediately be able to jump in.

2) Lua uses 1 instead of 0 as the base index into arrays etc.  This is out of sync with practically every other language in existence, and while not a major issue, it is something that should have been avoided. NOTE: you can use 0 if you want, it won't break, but most public code assumes a base of 1.



  Overall I currently feel that Lua is the superior language, and wish it were available for client site scripting of web pages.  Web programming seems to have evolved half-hazardly at best, and the only widely supported client side scripting language is JavaScript, so of course this is what I must use...

No comments:

Post a Comment