Tag Archive: javascript


I was looking at kangax‘s suggestions for Google Closure compression techniques and I wanted to know how UglifyJS handled it.

Input:
var i = 10000,
j = 0.1,
k = /[\w]/,
m = new Array(1, 2, 3);
throw 0.1;

Closure:
var i=1E4,j=0.1,k=/[\w]/,m=[1,2,3];throw 0.1;

UglifyJS:
var i=1e4,j=.1,k=/[\w]/,m=new Array(1,2,3);throw.1

Interesting that UglifyJS doesn’t compress the Array declaration, but does compress 2 other statements that Google Closure doesn’t/won’t.

In UglifyJS’s README it states that Google Closure “runs terribly slow”. Why is this an issue? Won’t you only compress when pushing to prod?

Wow. This is messed up. Here is a table of the browsers I have at my disposal and the returned value from the JavaScript variable “navigator.appVersion”.

Browser navigator.appVersion
Chrome 5.0 5.0
Opera 10.00 9.80
IE 8.0 4.0
IE 7.0 4.0
IE 6.0 4.0
Safari 4.0 5.0
Safari 3.2 5.0
Firefox 3.6 5.0
Firefox 3.5 5.0
Firefox 2.0 5.0

Ok browser makers. Let’s start over. I don’t think any site is using the navigator.appVersion anymore so now is as good of time as ever to start fresh. What do you say?

I didn’t know this!

“The null value … can only be assigned by your code.”
From Object-Oriented JavaScript by Stoyan Stefanov

This will alert “10″:
var num = num || 10;
alert(num);

But so will this:
num = 0;
var num = num || 10;
alert(num);

Watch out for the cases where num is “falsey“.

The following convert to false when applied to a double negation in JavaScript:
""
null
undefined
0
NaN

“… if you try to return anything [from a constructor] that is not an object [emphasis mine], the constructor will proceed with its usual behavior and return this.”
From Object-Oriented JavaScript by Stoyan Stefanov

I was reading Juriy Zaytsev‘s (@kangax) article “Optimizing HTML” and had a few comments.

1. I have used <!CDATA[...]> sections when I’m embedding “<” and “>” but I think that’s just because I’ve used XSL before and they have caused problems when performing transformations. But I’ll try to remove it from my HTML as he suggests.

2. If I don’t have a URL to use in the href attribute, I’ll still use “javascript:void(0)” because I really don’t like the hash being added to my URL. Perhaps I should just add some jQuery code:

$(“a[href='#']“).click(function(){return false;});

That’s 49 characters, so I would have to change only 3 links to use the hash instead of “javascript:void(0)” to save space.

I found a jQuery plugin to add the Konami code to your website, but I think mine is better. Mine works in IE8. Here’s a demo.

I came across a problem in Internet Explorer (it wasn’t a problem with Firefox) when I was trying to compare two strings. To me, one string looked to have an extra space in the front. No problem, I’ll just call the jQuery trim function. Well, that didn’t work. So I used charCodeAt and found it was ASCII character 160. I looked up char code 160 and saw that it is a “Non-breaking space”. You would think that a “space” character would be trimmed. I looked at the jQuery code that does the trimming and the grep pattern uses \s. So, evidently you can’t use \s to catch the “Non-breaking space” in IE. I wonder why no one else has seen this. I wrote up a test page to illustrate this.

To create the non-breaking space, you can use String.fromCharCode(160) or the Unicode representation “\u00A0″. “&#160;” doesn’t seem to work when using regular expressions, although it behaves the same when printed.

Browser Evaluates character 160 as white-space
Firefox 3, 3.5 Yes
Chrome 3.0 Yes
Internet Explorer 7, 8 No
Safari 3.2 No
Opera 9 Yes

Here’s my entry for the The Daily WTF‘s Programming Praxis: Russian Peasant Multiplication:

function peasant1(a, b) {
  var value = a & 1 ? b : 0;
  while(a > 1) {
    a >>= 1;
    b <<= 1;
    if (a & 1)
      value += b;
  }
  return value;
}

function peasant2(a, b) {
  var value = a & 1 ? b : 0;
  if(a > 1)
    value += peasant2(a >> 1, b << 1);
  return value;
}

function peasant3(a, b) {
  return a > 1 ? (a & 1 ? b : 0) + peasant3(a >> 1, b << 1) : a & 1 ? b : 0;
}

function peasant4(a, b) {
  return a & 1 ? value = a > 1 ? value = b + peasant4(a >> 1, b << 1) : b : value = peasant4(a >> 1, b << 1);
}