← home← blog

From jQuery to JavaScript and back again

30 Oct 2012

I saw an article posted on Netmag the other day for which I contributed a couple of coding tips. The tip I'm talking about in this post reads as follows:

Ben also thinks that falling back on frameworks and libraries isn't always the best answer. "If you're coding a small project and always, for example, include jQuery, think 'Can I do this with Vanilla JS?'" You may find that, indeed, you can do it in JavaScript, better and faster.

See, I wrote this tip back in July sometime (only now has it been published) so my thoughts have somewhat evolved since then. The line that struck me was the last one: "you can do it in JavaScript, better and faster" - the real truth is that, no, I can't. John Resig and his team at jQuery have put many, many man hours into optimising jQuery to the max, I don't think you'd find a line in the source that hasn't been put under a microscope and scrutinised, benchmarked and refactored.

I think the problem with my quote, apart from it being an over-generalisation, is that we are constantly being shown polar opposites in the code quality of people's jQuery. With some truly awful code to the other end of the spectrum with some compact, snappy code. jQuery can be used wildy and clumsily. The reason being is that it abstracts out a lot of the JavaScript "gotchas" into nice, easy function calls. Most errors are caught by jQuery and never seen by the user, but more dangerously, never seen by the developer. Someone will probably prove me wrong but in my experience, the bad code that I've seen (my early stuff definitely included) is largely using jQuery, as opposed to vanilla JavaScript. Coding in raw JavaScript teaches you to ration, it teaches you to be conscious of errors and mindful of constant optimisations.

My only piece of advice is to get a solid background in JavaScript, then move forward, appreciate the blood, sweat and tears Resig put into jQuery and use it to iron out those peculiarities that JavaScript often smacks us in the face with.

I'm available for hire

Hire me