<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
var $jq331 = $.noConflict(true);
</script>
<div id="box" style="background:#555;color:#FFF;position:absolute;left:0;top:0;width:400px;height:100px;</div>
var elem = document.getElementById('box');
console.log("elem", elem);
console.log("jq", $jq331);
var jqElem = $jq331('#box')
console.log("jqElem", jqElem);
elem.style.cssText +=';width:300px;height:50px;';
elem.style.width = '300px';
elem.style.height = '50px';
elem.style.top = '40px';
elem.style.left = '90px';
jqElem.css({ 'width': 300, 'height': 50, 'top':40, 'left': 90 });
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
cssText | |
style | |
jquery |
Test name | Executions per second |
---|---|
cssText | 112594.2 Ops/sec |
style | 440300.8 Ops/sec |
jquery | 116921.3 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net!
Benchmark Definition JSON
The provided JSON represents a benchmark definition, which consists of:
$jq331
by calling $.noConflict(true)
. The script also logs some console messages for debugging purposes.Individual Test Cases
There are three test cases in this benchmark:
style.cssText
property directly on the DOM element (elem.style.cssText +=';width:300px;height:50px;';
) versus not modifying the property at all (i.e., just reading it, which is expected to be faster).elem.style.width = '300px';\r\nelem.style.height = '50px';\r\nelem.style.top = '40px';\r\nelem.style.left = '90px';
) versus using the css()
method provided by jQuery (jqElem.css({ 'width': 300, 'height': 50, 'top':40, 'left': 90 });
).css()
method to set multiple CSS properties on the DOM element versus not using any library at all.Options Compared
The benchmark tests different approaches:
style.cssText
property (CSSOM) vs. reading its value and concatenating new styles (CSSOM)Pros and Cons of Different Approaches
Library Used
In this benchmark, jQuery is used as a library for setting CSS properties on the DOM element (jqElem.css({ 'width': 300, 'height': 50, 'top':40, 'left': 90 });
). The $.noConflict(true)
call is used to avoid conflicts with other libraries.
Special JavaScript Feature or Syntax
None mentioned in this benchmark.
Other Alternatives
If you're interested in exploring alternative methods for setting CSS properties, consider:
style
property to modify CSS styles.These alternatives can offer different trade-offs in terms of performance, flexibility, and feature set.