Skip to content

Instantly share code, notes, and snippets.

@brimelow
Last active December 17, 2015 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brimelow/5603596 to your computer and use it in GitHub Desktop.
Save brimelow/5603596 to your computer and use it in GitHub Desktop.
Performance difference between using jQuery.css() and the standard DOM style properties.
// get reference to a DOM element
var el = document.querySelector(".main-content");
// set some styles using jQuery.css()
$(el).css({
background: "#FF0000",
"box-shadow": "1px 1px 5px 5px red",
width: "100px",
height: "100px",
display: "block"
});
// now set via the native DOM style object
el.style.background = "#FF0000";
el.style.width = "100px";
el.style.height = "100px";
el.style.display = "block";
el.style.boxShadow = "1px 1px 5px 5px red";
// RESULTS: using jQuery.css() was 73% slower than native in Chrome
// Try the test yourself at http://jsperf.com/jquery-css-vs-native-dom
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment