Simplifying vs Complicating Performance Optimization

Simplifying your code for performance can and should be done throughout development. Complicating your code for performance should only be done after profiling has been done and bottlenecks have been identified.

During the entire development process it’s good to constantly refactor.  Reducing all extra steps and other cruft in a programs logic can not only improve performance but it makes your code more readable and decreases file size.  Reducing it to it’s poetic essence isn’t what the Knuth was talking about when he said “Premature optimization is the root of all evil.”

Once the code is working and perfomance analysis is done (I use dynatrace), certain bottlenecks appear.  Optimizing these issues sometimes requires complicating your code in order for it to perform better.  Here are some examples from a nice post by Doug Avery.  Replacing jQuery with native javascript can take out extra executions in a heavily trafficked loop but at the cost of increasing your code and making it less readable.  That’s why it’s critical that you only do this complicating type of optimization after performance analysis has been done and bottlenecks identified.

Reading JSON from Cookies

What’s the point in writing cookies if it’s hard to extract the values later.  I prefer to write JSON into my cookies.  This makes turning a cookie into a javascript object a very simple process.  I use jquery and the cookie plugin.

var json=($.cookie('the_cookie'));
var obj = $.parseJSON(json);

Now your cookie has been converted into a javascript object and you’re ready to rock.

On the other side, if you want to create a json on the client and write it to a cookie you could use json2.js as well as cookie.js