Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > PHP/MySQL/Scripting

  • Print
  • + Share This
This chapter is from the book

Abbreviate and Map

Another JavaScript optimization technique you can use to crunch your code is abbreviation and mapping. Abbreviation in JavaScript is more flexible than in HTML. In JavaScript, you can name your variables, functions, and objects anything you want, but HTML requires a fixed set of tag names, although class and id names can be abbreviated.

So instead of this:

function validateParseAndEmail()
var firstButton

Do this:

function email()
var button1

Or even better:

function e()
var b

Here's an example from's home page. Peter Belesis' original dual "news flipper" was self-describing ( It was also over 6.7K and slowed down our home page.

So instead of this (single-feed version):

<script src="/scripts/newsflipper.js">

...(newsflipper.js file below)

for(i=0;i<arTXT.length;i++) {
  arTopNews[arTopNews.length] = arTXT[i];
  arTopNews[arTopNews.length] = arURL[i];


function NSinit() {
  fad1 = new Layer(119);
  pos1 = document.anchors['pht1'];
  pos1E = document.images['phb1'];
  fad1.left = pos1.x; = pos1.y;
  fad1.clip.width = 119;
  fad1.clip.height =;
  fad1.bgColor = "#ffed9a";
  fad1.onmouseover = FDRmouseover;
  fad1.onmouseout = FDRmouseout;
if (IE4) {;

function FDRmouseover() {

We manually abbreviated and optimized the code by over 50 percent to less than 3K and used SSI to add it to the end of the page, like this:

<!--#include virtual="/f.js" -->

... (f.js file below)
function Ni(){fad1=new Layer(119);
function Fmv(){clearInterval(bT);}...

Moving the script from an external file in the head to an SSI at the end of the page saves one HTTP request, displays the body content sooner, and raises relevance. Of course, these terse abbreviations can be hard to read. One solution is to create a map of names and their abbreviated counterparts either manually or automatically with an optimization program (such as index.jsmap). Here's an example:

email  e
button1 b

I have yet to see an optimizer that can abbreviate to user-defined maps (although I'm told some are in development). Some optimizers abbreviate variables and objects automatically, but you're stuck with what the program chooses.

  • + Share This
  • 🔖 Save To Your Account