Loop unlooping in Javascript

Few days ago I’ve read a book ‘Even Faster Web Sites‘ about websites optimisation and I found one thing usefuluseful, not only on websites. There was a small tip about looploop unlooping. I want to quote them for later use.

First – with switch statement

var iterations = Math.ceil(values.length / 8);
var startAt = values.length % 8;
var i = 0;

do {
  switch(startAt) {
    case 0: process(values[i++]);
    case 7: process(values[i++]);
    case 6: process(values[i++]);
    case 5: process(values[i++]);
    case 4: process(values[i++]);
    case 3: process(values[i++]);
    case 2: process(values[i++]);
    case 1: process(values[i++]);
  }
  startAt = 0;
} while(--iterations > 0);

Second – without switch

var iterations = Math.floor(values.length / 8);
var leftover = values.length % 8;
var i = 0;

if(leftover > 0) {
  do {
    process(values[i++]);
  } while(--leftover > 0);
}

do {
  process(values[i++]);
  process(values[i++]);
  process(values[i++]);
  process(values[i++]);
  process(values[i++]);
  process(values[i++]);
  process(values[i++]);
  process(values[i++]);
} while (--iterations > 0);

I found second example more readable and I prefer it.
These examples after translation could be easily used in other scripting languages.

Tracking users by nickname on WordPress using Google Analytics

Some time ago I write article about tracking nicknames of users (from comments) on a WordPress blog with Piwik. This time I’m doing same but for Google Analytics.

I’m using Google Analytics plugin for WordPress so I’ve edited googleanalytics.php file to add some additional code for user tracking:

<script type="text/javascript">
var i,x,y,ARRcookies=document.cookie.split(";");
var comment_author = "";
for (i=0;i<ARRcookies.length;i++) {
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x.indexOf("comment_author") != -1 && x.indexOf("comment_author_email") == -1 && x.indexOf("comment_author_url") == -1) {
    comment_author = unescape(y);
  }
}
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-YOUR-UNIQ-NUMBER']);
_gaq.push(['_setCustomVar', 1, 'Nickname', comment_author, 1]);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>

Source: https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables?hl=pl