Last Updated: September 09, 2019
·
918
· gavinblair

Real Javascript

Don't be afraid of document.getElementById("myid") - it's much faster than $("#myid"). Use it as much as possible.

10 Responses
Add your response

I'd call premature optimisation on that one. By the time the difference in performance between jQuery and pure DOM becomes important, ditching the library altogether and replacing it with lighter alternatives should be the more logical solution.

over 1 year ago ·

That's nicely false. using jQuery to get an element return a jQuery selector element with all the capability of it (using jQuery methods, ...) but a document.getElementById just get a DOM element.

So Yes, using a $('#myID') to just retreive a DOM element is a bad practise, but telling everybody it's the same that a getElementById is totaly WRONG.

over 1 year ago ·

@clawfire He didn't say it was the same thing, only to not be afraid of it. People often rely too heavily on jQuery to do mundane tasks.

over 1 year ago ·

document.getElementById is 80% faster for me. Let me know what the results are for you: http://jsperf.com/id-selecter-v-getelementbyid @clawfire @passcod

over 1 year ago ·

@passcod I think learning to not be wasteful in the first place isn't premature optimization. document.getElementById is only marginally faster than $('#myId') yes, but by coding more efficiently as you go your app runs better and reduces compute cycles which is definitely important on mobile devices.

over 1 year ago ·

@gavinblair jquery is 77% slower for me.

over 1 year ago ·

@clawfire heehee, good thing I didn't tell everybody it's the same. I find that if you're just changing a style or updating the innerHTML it's faster to use native JS, although the syntax isn't as nice.

over 1 year ago ·

@somethingon I totaly agreed with you but it's important to notice it too ... without it it's like saying "hey lazer's saber is more powerfull to cut things than a knife" ...

over 1 year ago ·

I agree with @passcod. I always lookup a jsPerf for any iterating or complicated functions. Pure JS always beats frameworks, especially on mobile.

over 1 year ago ·

Just a few things:

The speed difference between $() and native DOM queries (getElementById, getElementsByClassName, querySelector(All)...) is incredible. Sizzle has more features. You might also plug in a different, faster, engine. That's not the point.

Mobile performance is incidental. Not everyone is using top-of-the-line Macbook Pros (I used to dev on an old laptop that was slower than my phone). Speed is good no matter what processor is running it; a faster application will often “feel” better.

Doing $(document.getElementById("thing")) is 4x faster than $("#thing") for the exact same result. (/re @clawfire, @gavinblair)

Better code design (i.e. learning to implement less wasteful code) trumps minor this-function-is-faster details. That's what I mean by premature optimisation. (/re @somethingon)

Pure JS doesn't always beat frameworks. In fact, mobile-oriented frameworks will often be more optimised than your own code. (/re @james2doyle)

over 1 year ago ·