optimizations to the javascript

Mostly just combining var statements and placing repeatedly used selected elements into variables, plus a rewrite of the _isInList function.