<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>lexanderA &#187; Utilities</title>
	<atom:link href="http://lexandera.com/category/utilities/feed/" rel="self" type="application/rss+xml" />
	<link>http://lexandera.com</link>
	<description>A blog about the web, mobile web, semantic web and mobile semantic web.</description>
	<lastBuildDate>Sun, 06 Jun 2010 18:27:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ultimate JavaScript minification</title>
		<link>http://lexandera.com/2009/05/ultimate-javascript-minification/</link>
		<comments>http://lexandera.com/2009/05/ultimate-javascript-minification/#comments</comments>
		<pubDate>Thu, 21 May 2009 09:00:23 +0000</pubDate>
		<dc:creator>Matjaž Lipuš</dc:creator>
				<category><![CDATA[Ideas]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Utilities]]></category>

		<guid isPermaLink="false">http://lexandera.com/?p=397</guid>
		<description><![CDATA[This is a guest post by Matjaž Lipuš, a social web geek, front-end web developer and semantic web enthusiast.
After developing some fat client applications using JavaScript frameworks I noticed how some code constantly repeats, making application footprint even bigger than necessary.
A while ago I (once again) stumbled upon article about helping the YUI Compressor. Article [...]]]></description>
			<content:encoded><![CDATA[<p><em>This is a guest post by <a href="http://matjaz.info" target="_blank">Matjaž Lipuš</a>, a social web geek, front-end web developer and semantic web enthusiast.</em></p>
<p>After developing some <a href="http://en.wikipedia.org/wiki/Fat_client" target="_blank">fat client</a> applications using JavaScript frameworks I noticed how some code constantly repeats, making application footprint even bigger than necessary.<br />
A while ago I (once again) stumbled upon article about <a href="http://yuiblog.com/blog/2008/02/11/helping-the-yui-compressor/" target="_blank">helping the YUI Compressor</a>. Article is great and in large client side applications could really make a difference in final footprint considering these tips. I think this should be automated as much as possible, so I extended a list of this tips which could be fully automated. And here comes in the <a href="http://developer.yahoo.com/yui/compressor/" target="_blank">YUI Compressor</a> (YUIC).<br />
I am using YUIC for code minification. I also contributed some fixes and suggestions to the author &#8211; <a href="http://www.julienlecomte.net/" target="_blank">Julien Lecomte</a>. Those were implemented in past versions. YUIC is a great tool for code minification and munging. I believe it can be further extended and improved generating even smaller output. So here are my tips how to achieve this, ordered by complexity.</p>
<h3>Concatenate static strings with +</h3>
<p>If you have a big chunk of text to insert into page you would probably split it into multiple lines and making multiple strings. YUIC already has functionality concatenating static strings into one. So code like this:</p>
<pre name="code" class="javascript">
var x = &quot;This is &quot; +
        &quot;some text&quot;;
</pre>
<p>becomes:</p>
<pre name="code" class="javascript">
var x=&quot;This is some text&quot;;
</pre>
<p>Don not use arrays for this, because YUIC cannot tell you are actually concatenating strings.</p>
<h3>Variable declarations at the beginning of the function</h3>
<p>Scope in JavaScript is per function. So if you declare a variable twice in the same function, second time variable is just overridden with new value. As JSLint suggests you should declare all variables at the beginning of the function. Looking at this from minification view this means only one var statement! So instead of:</p>
<pre name="code" class="javascript">
function iterate(array) {
    var result = &quot;&quot;;
    for (var i=0, len=array.length; i&lt;len; i++) {
        var el = array[i];
        result += el + &quot;,&quot;;
    }
    return result;
}
</pre>
<p>write:</p>
<pre name="code" class="javascript">
function iterate(array) {
    var result = &quot;&quot;,
        i, len,
        el;
    for (i=0, len=array.length; i&lt;len; i++) {
        el = array[i];
        result += el + &quot;,&quot;;
    }
    return result;
}
</pre>
<p>You should note when declaring global variables there is no need for var statement at all.</p>
<h3>This as local variable</h3>
<p>Writing serious JavaScript code means writing object-oriented code. In JavaScript we access object members in conjunction with this keyword. This means a lot of this keywords.</p>
<pre name="code" class="javascript">
Person.prototype.getInfo = function()
{
    var result = &quot;&quot;,
        i, len;
    for (i=0,len=this.length; i&lt;len; i++) {
        result += this[i].name + &quot; &quot; + this[i]. surname + &quot; - &quot; + this [i].age;
    }
    return result;
};
</pre>
<p>Assigning this to a local variable means YUIC can munge local variable to a 1-3 letters which means smaller footprint.</p>
<pre name="code" class="javascript">
Person.prototype.getInfo = function()
{
    var x = this,
        result = &quot;&quot;,
        i, len;
    for (i=0,len=x.length; i&lt;len; i++) {
        result += x[i].name + &quot; &quot; + x[i]. surname + &quot; - &quot; + x[i].age;
    }
    return result;
};
</pre>
<p>You should note local variable must be declared first, as we could access members of this object in the following variables declaration/assignment.</p>
<pre name="code" class="javascript">
Person.prototype.getInfo = function()
{
    var x= this,
        result = &quot;&quot;,
        i, len = x.length;
    for (i=0; i&lt;len; i++) {
        result += x[i].name + &quot; &quot; + x[i]. surname + &quot; - &quot; + x[i].age;
    }
    return result;
};
</pre>
<p>You should also note using this as local variable is only reasonable when this keyword occurs at least two times per scope, but that is most of the time.</p>
<h3>Shorten numbers</h3>
<p>Although this seems to be strange it is simple to trim leading zero from decimal numbers. So instead of:</p>
<pre name="code" class="javascript">
var n1 = 0.1,
    n2 = -0.1;
</pre>
<p>you should write</p>
<pre name="code" class="javascript">
var n1 = .1,
    n2 = -.1;
</pre>
<h3>Common strings</h3>
<p>As with JavaScript frameworks and it&#8217;s hash style configs there is a lot of string repetition.</p>
<pre name="code" class="javascript">
var form = new MyForm({
    id: &quot;form&quot;,
    items: [{
        type: &quot;text&quot;,
        cls:  &quot;blue&quot;,
        name: &quot;name&quot;
    }, {
        type: &quot;text&quot;,
        cls: &quot;blue&quot;,
        name: &quot;surname&quot;
    }]
});
</pre>
<p>This code can be minified (in a inner scope) as:</p>
<pre name="code" class="javascript">
var textString = &quot;text&quot;,
    blueString = &quot;blue&quot;,
    form = new MyForm({
    id: &quot;form&quot;,
    items: [{
        type: textString,
        cls: blueString,
        name: &quot;name&quot;
    }, {
        type: textString,
        cls: blueString,
        name: &quot;surname&quot;
    }]
});
</pre>
<p>A special caution must be given to a strings declared as variables as they can be changed. So we cannot use this variable as a constant. These two strings should be separated into two variables unless we are sure value has not been changed until constant string usage.<br />
We could go even further by moving common strings to outer most scope which uses this string and accessing it in inner scopes with closures and even further replacing substrings (in this case »name«). This raises a question about performance penalties doing string concatenation.</p>
<h3>Repeating access to object members</h3>
<pre name="code" class="javascript">
function focusInput()
{
	if (this.msgInput) {
		this.msgInput.focus();
	}
}
</pre>
<p>this could without any problem been written as</p>
<pre name="code" class="javascript">
function focusInput()
{
	var msgInput = this.msgInput;
	if (msgInput) {
		msgInput.focus();
	}
}
</pre>
<h3>Shorten object&#8217;s member access</h3>
<p>JavaScript libraries have a nice namespace convention, but in a large usage of library you get a lot of code repetition.</p>
<pre name="code" class="javascript">
var store  = new Lib.data.Store(),
    record = new Lib.data.Record();
store.add(record);
</pre>
<p>We can see a common namespace is Lib.data which can be shorten by assigning to a variable</p>
<pre name="code" class="javascript">
var Data   = Lib.data,
    store  = new Data.Store(),
    record = new Data.Record();
store.add(record);
</pre>
<p>There could even be further improvements if we are dealing with (static) object which does not have this key word in the function. Good example is the utility function JSON.stringify(), which can be assigned to a local variable.</p>
<pre name="code" class="javascript">
var toJSON  = JSON.stringify,
    result1 = toJSON(obj1),
    result2 = toJSON(obj2);
</pre>
<h3>Shorten object&#8217;s member names</h3>
<p>While using namespaces is good in an organizational way it means a lot of code for just accessing object. The idea is to shorten object&#8217;s member names. So instead of</p>
<pre name="code" class="javascript">
function Person(name, surname)
{
    this.firstName = name;
    this.lastName  = surname;
}
Person.prototype.getFullName = function()
{
    return this.firstName  + &quot; &quot; + this.lastName;
};
</pre>
<p>code looks like</p>
<pre name="code" class="javascript">
function Person(name, surname)
{
    this.a = name;
    this.b = surname;
}
Person.prototype.c = function()
{
    return this.a + &quot; &quot; + this.b;
};
</pre>
<p>This is only reasonable for automated process, although keeping short member names can also save a lot of bytes.<br />
You should keep in mind that you must replace member access in all places, this means where they are defined (set) and used. You also cannot shorten reserved members such as Array&#8217;s length or documents getElementsById.<br />
So a good idea would probably be keeping a white list of members which should (not) get munged.</p>
<p>I was already doing some work in YUIC about variable declarations at the beginning of the function, shorten numbers and common strings, but it is not 100% stable, although I successfully minified some of the JS libraries.<br />
Also I did not measure any performance penalties of these tips, but it would be interested to see some.<br />
You should note that this would best perform if code is also <a href="http://jslint.com/" target="_blank">JSLint</a> compliant.</p>
]]></content:encoded>
			<wfw:commentRss>http://lexandera.com/2009/05/ultimate-javascript-minification/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A pixel measurning device</title>
		<link>http://lexandera.com/2009/01/a-pixel-measurning-device/</link>
		<comments>http://lexandera.com/2009/01/a-pixel-measurning-device/#comments</comments>
		<pubDate>Thu, 01 Jan 2009 23:00:56 +0000</pubDate>
		<dc:creator>Aleksander Kmetec</dc:creator>
				<category><![CDATA[Utilities]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[Open source]]></category>
		<category><![CDATA[ruler]]></category>
		<category><![CDATA[sharpdevelop]]></category>
		<category><![CDATA[Source code]]></category>

		<guid isPermaLink="false">http://lexandera.com/?p=57</guid>
		<description><![CDATA[If you type &#8220;free on screen ruler&#8221; into Google, you get at least 10 useful results back. If only it was like that back in late 2004 when I actually needed one! But it wasn&#8217;t, so I had to spend a weekend writing my own, using an early version of Sharp Develop which liked to [...]]]></description>
			<content:encoded><![CDATA[<p>If you type &#8220;free on screen ruler&#8221; into Google, you get at least 10 useful results back. If only it was like that back in late 2004 when I actually needed one! But it wasn&#8217;t, so I had to spend a weekend writing my own, using an early version of <a href="http://www.icsharpcode.net/OpenSource/SD/">Sharp Develop</a> which liked to freeze, crash, and explode a lot. Fun times.</p>
<p>But despite all the difficulties, I managed to finish it. And only 4 short years later it&#8217;s finally available to anyone desperate enough to be actually willing to use it, for free.</p>
<p>It&#8217;s called yaRuler (yet another ruler &#8211; yes, very original, I know) and it looks like this:</p>
<div id="attachment_43" class="wp-caption alignnone" style="width: 460px"><img class="size-full wp-image-43" title="A simple screenshot" src="http://lexandera.com/wp-content/uploads/2009/01/ruler.png" alt="What a ruler might look like" width="450" height="70"><p class="wp-caption-text">What a ruler might look like</p></div>
<p>Full feature list, screen shots, downloads and source code are available at the <a href="/yaruler/">yaRuler project page</a>.</p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/36fda014-3d47-4445-9994-50aac982aea4/" title="Zemified by Zemanta"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=36fda014-3d47-4445-9994-50aac982aea4" alt="Reblog this post [with Zemanta]"></a></div>
]]></content:encoded>
			<wfw:commentRss>http://lexandera.com/2009/01/a-pixel-measurning-device/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
