Monthly Archives: June 2011

Sometimes jQuery is (not) the Answer

It’s difficult to say, as much as I love jQuery I must admit that it is at times the wrong route to take when addressing a problem.

While looking over a few questions on StackOverflow today I came across a poster who was seeking to modify the width of all nested elements within a container – he naturally thought jQuery would be a good, simple solution – and it is. But while it may be simple, it’s wrong in this case.

Rather than coming right out and answering with something like:

$("#container").find("*").width("100%");

I instead took just a moment to whip up a test case on http://jsperf.com (great site for testing the performance advantages of one method over others.) Just as I had suspected, jQuery would be slower than raw JavaScript – much, much slower.

var i,
    container = document.getElementById("container"),
    tags = container.getElementsByTagName("*"),
    total = tags.length;

for ( i = 0; i < total; i++ ) {
  tags[i].style.width = '100%';
}

This simple block does the same thing as the aforementioned jQuery code, but it’s much faster. In fact, in the time it takes the jQuery code to perform 20k operations, the raw JavaScript code performs nearly 300k!

You can see this particular performance test online at http://jsperf.com/resizing-children.

code-igniter-logo-black

Todays WTF Moment with CodeIgniter, MySQL and PHP

As routine as an oddly-twitching finger sabotaging any attempt to use your mouse, strange things  may take place with your environment, tools, or code. Today I was working on setting up a very small project with CodeIgniter when I came across the task of establishing database credentials so I could auto-load my resources and have a fresh connection waiting on me hand and foot.

Opening up application/config/database.php I went ahead and placed my hostname, username, password, and database name. Almost done, right? Next I went to application/config/autoload.php and updated the $autoload['libraries'] array to include “database”. Done, right?

To my surprise I had a whole storm of agony just waiting on me to try and pull some results from my model. When I did just that…

This took me back just a moment – I had put in the right data, right? I’m working on “localhost”, and my db user is “root” with a blank password. How exactly do you screw that up? After double-checking, my credentials showed to be valid. So I went to make sure I was auto-loading the database properly; perhaps I said “datbase” instead of “database”? Who knows, it’s happened before. Nope, I had this value correct as well.

It’s around this moment Google must necessarily be opened and error messages must necessarily be pasted into the search bar. Anytime I have problems like these, which turn me to Google, I’m reminded at just how unreliable forums are. I inevitably ended up looking at about 12 different forums, where people can post whatever the flip they think might be the problem and you have very little reason to doubt them in your ignorance.

“Did you provide your port number?” “Did you update your hosts file to associate ‘localhost’ with 127.0.0.1?” “Did you max out your connections?” All of these where the types of questions being asked, and yet none of them did me any good. My host file is fine – I run plenty of websites. I’m not maxing out my connections either – I’m the only sap accessing the laptop! It wasn’t until I came across a PHP bug filed in 2008 regarding PHP 5.3’s inability to use MySQL functions when using “localhost”.

Now, this bug submission was eventually marked as “bogus,” but it wasn’t entirely useless because it got me to explore another route in my troubleshooting. Out of curiosity I looked down to my instance of WAMPServer to see what version of PHP I was running; 5.3.1.

Out of curiosity, I switched to 5.3.5. My server went offline, restarted, and was back up and running in just a matter of seconds. Time to try my CodeIgniter app again…Bingo!

The connection issues were resolved by switching from 5.3.1 to 5.3.5. Just for some environmental facts and info, I’m running WAMPServer 2.0, Apache 2.2.11, MySQL 5.1.33 and PHP 5.3.1 (Well, 5.3.5 now). The CodeIgniter version is 2.0.2.

At this point, I still have no idea what causes this problem but I am sure glad it’s done with so I can get back to work. If you’re having a similar problem, on a similar setup, perhaps this will help you as well.

If anybody knows what actually causes this problem, I’d love to know in the comments below.