Search code examples
javascriptjqueryhashchangebrowser-state

JavaScript .hashchange performance. Can it bring any slowdown?


jQuery hashchange event

For me it looks like most mature solution right now(please correct me if I'm wrong). I really like this plugin for manipulating with browsers hashes. It simplifies js code a lot in some cases.

I really want to start use it extensively but I have a question for you.

Accordingly to the source it uses loop and check whether hash anchor was changed every 50 ms.

What about performance? Can I overuse hashchange? Can it lead to some significant slowdown in performance? If so in which cases?


Solution

  • Checking a simple string property every 50ms is an infinitesimal cost compared to probably everything else you're running, I wouldn't be concerned about performance here. If you're changing the hash often and your callback is very, very expensive then deal with that (you callback), but the check itself is a very, very small cost.

    Also keep in mind that 50ms check is only for browsers that don't have window.onhashchange built-in, for those it's a native event (and that's most modern browsers).