Closure Compiler is smarter than you

I’ve known about Google’s Javascript minifier, Closure Compiler, for awhile, but after sending a couple snippets through it today I realized it’s a pretty impressive piece of work.

Input:

function howManyMatch(arr, pattern) {
   var l = arr.length;
   var total = 0;
   var i = 0;
   for (; i < l; i++) {
       if (pattern.test(arr[i])) {
           total++;
       }
   }
   return total;
}

Output:

function howManyMatch(b,d){for(var e=b.length,c=0,a=0;a<e;a++)d.test(b[a])&&c++;return c};

Or with whitespace re-added:

function howManyMatch(b, d) {
    for (var e = b.length, c = 0, a = 0; a < e; a++)
        d.test(b[a]) && c++;
    return c
}

Optimizations:

  • shortened parameter names and removed unneeded semicolons (YUI Compressor already did this much)
  • combined multiple var statements into one declaration
  • moved that declaration into the first section of the for loop (to save one semicolon!)
  • removed brackets around blocks with only one statement
  • replaced if statement with && operator (to save two bytes)

Several of these are often done by JS devs by hand. With closure compiler on tap, we no longer have to. Just concentrate on readability and maintainability and let CC worry about size optimization.

Less is More: Billie Davis 1969

“Nobody’s Home to Go Home To” was a 1969 B-side for Billie Davis that I have a weakness for. The bass playing is incredible and the song cleverly jumps between three keys, but the strings and backing vocals kind of take over the recording. I noticed this morning that they’re both panned hard right and the vocal is centered, so I got to work.

  • Dumped the right channel to make a mono track of the left, leaving all the essentials: drums, bass, piano, elec & acoustic guitars, a quiet organ, tambourine, and the vocal.
  • Made several surgical cuts to bass frequencies that took over the mix at points.
  • Mitigated some incidents of “breathing” and “pumping” in the breaks. This is where a compressor had turned up the gain while the band’s last note of a section was fading out. This can be done to great effect (after the snare hit at 0:21 in Elvis Costello’s “Busy Bodies”), but on this track it just sounded like a someone with coffee jitters was leaning on a fader, and it made the snare hits that preceded the following sections unnaturally loud.
  • Raised some high frequencies to bring some sparkle to the vocal
  • Added a tiny bit of stereo echo to widen the sound

In the result, you get a more interesting intro (IMO) and a tighter rhythm section, and you can actually hear the piano, the backbeat snaps of the electric guitar, what sounds like a low temple block on the snare hits, and Billie’s quiet falsettos at the end of the choruses.

Billie Davis – Nobody’s Home to Go Home To (mrclay.org mix)

[audio:http://www.mrclay.org/wp-content/uploads/2010/12/Billie-Davis-Nobodys-Home-To-Go-Home-To-mrclay-remaster.mp3|titles=Billie Davis – Nobody’s Home To Go Home To (mrclay remaster)]

Here’s the original .

Kristof on Haiti & Trade

I can’t agree more.

Ultimately what Haiti most needs isn’t so much aid, but trade. Aid accounts for half of Haiti’s economy, and remittances for another quarter — and that’s a path to nowhere.

The United States has approved trade preferences that have already created 6,000 jobs in the garment sector in Haiti, and several big South Korean companies are now planning to open their own factories, creating perhaps another 130,000 jobs.

“Sweatshops,” Americans may be thinking. “Jobs,” Haitians are thinking, and nothing would be more transformative for the country.

Let’s send in doctors to save people from cholera. Let’s send in aid workers to build sustainable sanitation and water systems to help people help themselves. Let’s help educate Haitian children and improve the port so that it can become an exporter. But, above all, let’s send in business investors to create jobs.

Otherwise, there will always be more needs, more crises, more tragedies, more victims. Back in the cholera treatment center here in Mirebalais, health workers were still disinfecting the bed on which Mr. Merilus had died when, in the tent next door for milder cases, a middle-aged woman suddenly collapsed.

Nurses splashed water on her face but could not revive her. So they rushed her to the main cholera hospital tent to take the newly vacant bed there.

And that is the brutal cycle of poverty in Haiti that only jobs and trade can break.

Bring Back TSA Classic

At this point very few would expect that you should be able to carry metal items undetected onto a plane, so I don’t have too big a problem with metal detectors at airports. However, society has a general expectation that the image of one’s naked body is private, so we should certainly consider the new scanners a “search” under the Fourth Amendment, requiring some level of probable cause. Is showing up for a flight probable cause for suspicion that you have explosives on you?

Setting aside the question of constitutionality, there seems to be mounting evidence that TSA agents cannot handle the responsibility that this much power brings:

What happens when a terrorist successfully sneaks in bomb materials embedded under the skin? I’d guess the attack would fail for the same reason the underwear bomber’s did: our system already worked. Until all international airports with flights to the U.S. are outfitted with the new scanners and procedures, we still can’t prevent another underwear bomber (he boarded in Amsterdam).

?YouWontSeeMe

In beta 11, Opera’s going to hide all “http(s)://”, and also all querystrings (until you focus the addressbar). Opera’s devs are right that users consider them mostly “gibberish”, but I think this change could cause a ton of problems and confusion for people, especially support staff, and there are plenty of sites/apps still out there with URLs based on querystrings. I can see this setting as short-lived.

We need a frontend design tool for live web pages

It’s quite frequently that an HTML/CSS designer might want to make changes to a live web page. Maybe she doesn’t have write access, or maybe the fixes needed aren’t worth the start-up cost of copying the page locally and working on it there, or multiple designers want to work up ideas on a given page simultaneously. The “Inspect Element” capabilities of most modern browsers will let you make HTML/CSS changes to a live page, but navigating or refreshing causes those changes to be lost, and they’re hard to keep track of.

Here’s a feature wishlist:

  1. keep track of “Inspect Element” user changes with the ability to save them locally
  2. keep changes in discrete “changesets” that can be re-applied, or saved as a unified patch of the original files, or at least as the modified original files
  3. allow swapping page CSS files with user-controlled CSS files (e.g. file:/// or htttp://localhost/)
  4. allow swapped CSS files to be periodically “refreshed” so the user wouldn’t have to switch between CSS editor and browser.

Firefox’s Stylish extension looks to come close to (3), allowing you to add user styles to a page/site, like Greasemonkey does for Javascript. It doesn’t look like the editing experience is great, though.

You can manually do (3) and use the CSS Reload Every bookmarklet, but it reloads all CSS, which is kinda of jarring.

A bookmarklet could implement (3) and (4) and persist its settings in a cookie.

Other ideas?

Update: Here’s a bookmarklet that let’s you swap CSS files and stores its settings in a cookie for the current page. Next step is for it to allow adding files and controlling the cookie path.

You must enable Javascript! (right-click, add to favorites or bookmarks)

(CSSswap source).

“Buy American”

When economies are struggling, protectionism seems well-intentioned: By “buying American” we can go back that golden fantasy age when everything was American-made and everyone had a decent-paying job and could afford the latest luxuries. I have some examples that I hope can convince you that freer trade benefits everyone. It’s not completely intuitive; we commonly think that one person is always screwed in a deal; if it’s good for [insert foreign country], it must be bad for America. Continue reading  

Email Address Munging Still Mitigates Harvesting

At least a decade into the use of simple email munging on web pages (my own solution here) there’s a growing consensus—and it seems like common sense with the advancement of the web in general—that this surely can’t still work on modern harvesters. While trying to look up some old research I’d read about this, I came across this January 2010 post by a Project Honey Pot admin:

We’ve run tests on different munging techniques and find that they are still surprisingly effective. We have a page that has a set of differently munged addresses. The ones protected by Javascript have yet to receive a single message. Even simple things like using ASCII character encoding to hide the @ sign, or adding spaces to the addresses, is surprisingly effective at stopping harvesting.

Whether to munge or not is kind of a religious war, but I see this as pretty convincing evidence that munging isn’t obsolete just yet. Of course this or any other single prevention strategy isn’t a “solution”, but mitigation of the inevitable is still arguably worthwhile, especially for people without the luxury of having top notch admins run their mail server.