Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleaner faster JavaScript, Replacing jQuery with JavaScript

Like many folks I learned JavaScript by learning jQuery.

Lately I have been replacing bits like:

$(this).attr('title') with this.title

$(this).attr('id') with this.id

$(this).val() with this.value

$(this).parent() with this.parentNode

$(this).attr('class') with this.className

Not only is my code cleaner but technically faster.

  1. Is this type of reduction acceptable and encouraged?

  2. Are there any other common practices I should be doing in raw plain JavaScript instead of jQuery?

  3. Are there any potential cross browser issues with this type of reduction-ism?

like image 338
iambriansreed Avatar asked Aug 03 '12 15:08

iambriansreed


1 Answers

Whilst using native Javascript functions are generally faster than their jQuery counterparts it does expose you to any browser compatibly issues that may arise from their use. this.value and such is unlikely to cause problems but other similar attributes / functions may well not work in all browsers. Using a framework like jQuery means you dont have to deal with, or worry about, such things.

I would only ever use plain Javascript if performance is an issue i.e. you have a lot of tight loops and repeated operations.

like image 126
Chris Avatar answered Oct 22 '22 17:10

Chris