Avoid jQuery.bind()

When chasing down performance issues, you never know what kind of problems you will find. I was looking for something that would cause jitter when scrolling on the page. After looking at the custom code that runs on every scroll event, I still had not found a reason for the jitter. Looking at the JavaScript CPU profile when scrolling in Chrome showed that there was an overwhelming majority of the time spent in a function in the Prototype JS library.

On close inspection of Prototype JS, it extends the base Object object with a bind method. While my code uses the jQuery().on() method to declare event handlers, we were utilizing a plugin that uses jQuery().bind() instead. Adding breakpoints to this code made it obvious that for each scroll event that was fired, Prototype JS was hijacking the event, and running it through its processes instead of jQuery handling it, causing much more CPU usage than is normally expected.

Updating this plugin to use jQuery().on() fixed one part of the problem. Unfortunately, this was not the only spot where this plugin had issues. While many know it is a good idea to namespace your JavaScript variables and functions, that is not where the namespacing ends. If you create your own custom events that you are triggering and handling with event handlers, you should also namespace the event names. Without it, a simple triggering of appear, which means nothing in jQuery, suddenly triggers events that Prototype and Scriptaculous automatically bind to and execute code when they are fired.

Effectively, you should make sure you only use jQuery().on() and always namespace your event names and variables.

Related Posts

Mar 31, 2014
2 minutes

JavaScript Templating

Many times it becomes useful to be able to make an AJAX request for some data, insert it into some HTML that is already on the client, and then display it to the user. There are a few ways to implement this, each approach has its benefits and drawbacks.

String Concatenation

Possibly the simplest way to accomplish the templating in JavaScript is to use simple string concatenation with ‘+’. This is the approach that I see many newcomers to JavaScript use in their code, as it is the simplest to implement. However, it does have a major drawback in that this method has the worst performance of all, especially in older versions of Internet Explorer. This could be implemented as below:

Apr 25, 2014
4 minutes

Optimizing Website Load Time

Assuming you have already done a few things to improve the page load time of your website, such as using a Varnish caching server, GZipping your content in transit, minifying that same content, and turning on all caching options that Magento or your web platform of choice have available, there is still more you can do. When it comes to website performance, the 80/20 rule definitely applies. 80% of the performance tweaks that you perform will only provide a miniscule improvement to the site load time, while the 20% of things you do make a big difference. Any time I am looking to speed of the performance of a website, I seek those 20% items that give you the biggest bang for your buck.

Apr 28, 2014
2 minutes

Improve jQuery Performance With $().addClass()

When looking at things that make a website seem sluggish, you might assume that the most popular JavaScript framework out there always does things in the most efficient manner. However, as I have found, jQuery does not always produce the best performance due to it having to support many different browsers with version 1.x. As a general rule, instead of setting CSS attributes directly on the selected nodes, I prefer to instead add and remove classes on those nodes instead, as it seems to perform much better.