I was refactoring some code this morning for a plugin I’m writing and ran into a case where I needed to dynamically execute a jQuery method. I had a variable that holds the value of a jQuery function I wanted to execute based on which key code was pressed; prev() for the up arrow, and next() for the down arrow. As it turns out you can do this with array notation.
Archive for December, 2009
Executing jQuery Methods Using Array Notation
Thursday, December 10th, 2009JavaScript Loop Performance: Caching the ‘.length’ Property of an Array
Wednesday, December 9th, 2009I was reading this article from SitePoint earlier about how inefficient the JavaScript is behind Google’s new Closure library, and saw some interesting discussion about caching an array’s length property before entering a ‘for’ loop. This is what I’m talking about:
// uncached length property for(var x=0; x < myArray.length; x++){} // cached length property for(var x=0, len=myArray.length; x < len; x++){}
I saw this tip and didn’t think twice about it; I’ve always been told that caching the length property is indeed faster. I then saw a couple comments saying otherwise:
jQuery Related Selects Plugin 0.3
Wednesday, December 2nd, 2009I’ve just pushed version 0.3 of my related selects jQuery plugin to GitHub. Along with refactoring the code and some minor performance improvements, 0.3 allows you to return data in HTML format instead of JSON. This gives you direct control over the markup and individual option tags in case you want to use optgroups, assign classes to options, etc.
I’m still undecided whether or not I want to add caching. I thought about it when I first wrote it and a user suggested it, but I cannot picture a use case where caching would be necessary; people are likely to choose an option and either decide it was incorrect and change it, or stick with it. In either of these cases, caching doesn’t do anything other than add overhead. If a user selects an option and then continuously changes/reverts back to it, caching would be helpful, but how often is this likely to happen?
Anyways, here’s she is: