<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<style>
#wrap1 {
width:300px;
margin:10px;
padding:10px;
border:10px solid black;
}
#wrap2 {
margin:10px;
padding:10px;
border:10px solid red;
}
#myDiv {
margin:10px;
padding:10px;
border:10px solid blue;
}
</style>
<div id="wrap1">
<div id="wrap2">
<div id="myDiv">
Test
</div>
</div>
</div>
var myDiv = document.getElementById('myDiv');
var $myDiv = $(myDiv);
var getStyle2;
// getComputedStyle isn't compatible with all older browsers (notably IE),
// so this is a wrapper function that works with those browsers.
// From http://www.quirksmode.org/dom/getstyles.html
function getStyle(el, styleProp) {
if (el.currentStyle)
var y = el.currentStyle[styleProp];
else if (window.getComputedStyle)
var y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
return y;
}
if (window.getComputedStyle) {
getStyle2 = window.getComputedStyle;
} else {
getStyle2 = function(el) {
return el.currentStyle;
}
}
var npaddingTop = $myDiv.css('padding-top');
var npaddingTop = getComputedStyle(myDiv).paddingTop;
var npaddingTop = getStyle(myDiv, 'paddingTop');
var npaddingTop = getStyle2(myDiv)['paddingTop'];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
jQuery.css | |
getComputedStyle() | |
getStyle() | |
getStyle2() |
Test name | Executions per second |
---|---|
jQuery.css | 127237.3 Ops/sec |
getComputedStyle() | 320574.8 Ops/sec |
getStyle() | 547816.6 Ops/sec |
getStyle2() | 472341.1 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
What is being tested?
MeasureThat.net is testing four different ways to retrieve CSS styles in JavaScript: getComputedStyle
, getStyle
, getStyle2
, and jQuery's .css()
method. Specifically, it's measuring the execution time of each approach for retrieving a particular style property (padding-top
) from an HTML element.
Options compared:
getComputedStyle(myDiv).paddingTop;
: This approach uses the native Web API to retrieve the computed style of an element.getStyle(myDiv, 'paddingTop');
: This approach is a wrapper function provided by the test case, which attempts to use the currentStyle
property for older browsers that don't support getComputedStyle
.getStyle2(myDiv)['paddingTop'];
: Similar to getStyle
, but uses the window.getComputedStyle
variable assignment instead of a separate function.$myDiv.css('padding-top');
: This approach uses jQuery's CSS manipulation method to retrieve the value of a style property.Pros and Cons:
getComputedStyle(myDiv).paddingTop;
:getComputedStyle
.getStyle(myDiv, 'paddingTop');
:currentStyle
property.getComputedStyle
due to the additional overhead of the wrapper function.getStyle2(myDiv)['paddingTop'];
:getStyle
, but uses a separate variable assignment instead of a function call. This might make it slightly faster or more efficient in some cases.getStyle
.$myDiv.css('padding-top');
:Other considerations:
getComputedStyle
is not compatible with older browsers (notably IE). This is why getStyle
and getStyle2
are used for those cases.getComputedStyle
) are generally faster than the wrapper functions (getStyle
, getStyle2
). However, these differences might be negligible in practice.Alternatives:
If you're looking for alternative approaches to retrieving CSS styles in JavaScript, consider using other libraries or frameworks like:
window.getElementStyle()
: Another method provided by WebKit-based browsers to retrieve the current style of an element.CSS
API: The new CSS API provides a set of methods for manipulating and accessing CSS styles.