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.

comments powered by Disqus

Related Posts

jQuery.hover Issues in Internet Explorer on Windows 7

When creating a new mega-dropdown menu for a site I was working on, I used jQuery’s .hover event to trigger which content the menu was displayed. This seemed to work as expected in most browsers that I tested in, except for one, Internet Explorer. Unfortunately, it wasn’t even in every instance of Internet Explorer. Windows 7 After Windows Vista came out as one of the biggest duds that the world has ever seen, Windows 7 was a ringing success.

Read More

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.

Read More

Hover Effects in JavaScript?

One of the things that can be annoying when looking at someone else’s code is when a more complex technology is used to solve a problem that can be handled more simply with another method. An example of this is when you utilize JavaScript to implement a hover effect on some elements. I know of one scenario when you would need JavaScript to trigger a hover effect, and that would be when you want to trigger the hover effect with a touch event.

Read More