4

Jquery allows us to use the $.css() method directly and pass the raw DOM element as the first parameter. For example, if I want to set 'a' to the width of myDiv, jQuery allows this syntax:

(OPTION 1):

var elem = document.getElementById('myDiv');
var a = $.css(elem, 'width');

instead of this (OPTION 2):

var a = $('#myDiv').css('width');

Option 1 does not require a selector, and it appears to rely on the global jQuery object instead of creating a new one. I can't find any documentation in the jQuery API or online about this method. I assume this would be a performance increase, especially when jQuery objects are required in animations. Any reason why I shouldn't be using this method?

Thanks!

EDIT: Perf tests show that option 1 is a bit faster. Doesn't seem like there's any reason not to use $.css() directly. Thanks to all for the answers!

5
  • 1
    document.getElementById('myDiv').style.width would probably be even faster, so why not use that ? Commented May 5, 2013 at 2:02
  • Yet another option would be $(document.getElementById('myDiv')).css('width') (though I doubt that would be faster than the first one). Commented May 5, 2013 at 2:04
  • In the simplified example above you would be correct, but I am creating my own custom CSS hooks, and the native JS will not work. My second option works with custom CSS styles. Commented May 5, 2013 at 2:08
  • So you can get the element with a native method, pass that element to a jQuery method that returns the value of a set style, but you can't use the native method to get the same style since you're using custom hooks. Seems like a strange way to do things to me, but whatever ? Commented May 5, 2013 at 2:11
  • That's right. If I have a custom CSS style like 'backgroundPositionY', the native JavaScript cannot access it; so I must use jQuery's .css(). Commented May 5, 2013 at 2:38

3 Answers 3

1

Yeah, the first one is slightly faster. But it can only get css value.

I made a perf test here: http://jsperf.com/jquery-css-signature You may revise it and test out other options.

Now that the facts are set, at these level of performance optimisation, I don't think it worth the overhead. Go with the more clear/maintainable way of getting the same result. The performance benefit ain't big enough to really matters in most case.

Sign up to request clarification or add additional context in comments.

2 Comments

Well, it looks like the $.css([element]) is 13% faster than $([selector]).css(). Not too much. I ran it with more divs and got about the same result.
Well, 13% could mean a lot on longer operation. But these two make a lot of operations per second, so yeah, the performance gain don't mean much.
1

Anything that takes jQuery out of the equation makes it faster. By using the native getElementById instead of passing a string to be processed, you increase the speed.

Even faster would be to use the native getComputedStyle:

var elem = document.getElementById('myDiv');
var a = window.getComputedStyle(elem).width;

Note that older versions of IE use currentStyle instead, so you can normalise it like so:

window.getComputedStyle = window.getComputedStyle || function(e) {return e.currentStyle;};

For more information on using plain JavaScript, check out Vanilla JS

1 Comment

I like the Vanilla JS website. Just a LITTLE sarcasm there.
0

Option 2 will, after some type checking and parsing, do roughly the same thing as option 1. I can't imagine the performance difference (if any) is significant.

If you're worried about performance when referencing an element repeatedly, your best bet is to do all the lookups once, and reuse that:

var elem = $('#myDiv');

var a = elem.css('width');
elem.css('height', '100px');
// etc.

2 Comments

That's good to know. My specific application is I'm writing a plugin to do vertical (y axis) background animations. The animation gets the background-position style at each step of the animation, so I'm trying to reduce the overhead as much as possible.
I already did this for you : stackoverflow.com/questions/12340130/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.