Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set styles comparison
(version: 0)
Comparing performance of:
1 vs 2 vs 3 vs setAttribute
Created:
3 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div id="test"></div>
Script Preparation code:
el = document.getElementById("test"); props = { height: '1vh', width: '1vw', color: 'red', border: '1vmin solid red', 'background-color': 'black', padding: '0.5vmin' };
Tests:
1
let i = 0; while (i < 10000) { let css = ''; for (const k in props) { css += `${k}:${props[k]};`; } el.setAttribute('style', css); i++; }
2
let i = 0; while (i < 10000) { el.setAttribute('style', Object.entries(props).map(x => x[0] + ':' + x[1] + ';').join('')); i++; }
3
let i = 0; while (i < 10000) { let css = ''; let ents = Object.entries(props); for (let ii = 0; ii < ents.length; ii++) { css += `${ents[ii][0]}:${ents[ii][1]};`; } el.setAttribute('style', css); i++; }
setAttribute
let i = 0; while (i < 10000) { el.setAttribute('style',"color:red;border:1vmin solid red;padding:0.5vmin;background-color:black;height:1vh;width:1vw;"); i++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
1
2
3
setAttribute
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents a JavaScript microbenchmarking test case for comparing the performance of different approaches to set CSS styles on an HTML element. **Benchmark Definition:** The benchmark is defined in two parts: 1. **Script Preparation Code**: This code snippet creates a JavaScript object `props` that contains various CSS style properties (e.g., height, width, color, border, background-color, padding) and assigns it to a variable `el`. The variable `el` represents an HTML element with the ID "test", which is created using the HTML Preparation Code. 2. **Html Preparation Code**: This code snippet creates a basic HTML structure with a `<div>` element having the ID "test". **Individual Test Cases:** The benchmark consists of four test cases, each defined in a separate JSON object: 1. **Test Case 1 ("1")**: This test case uses a traditional `for...in` loop to iterate over the `props` object and construct the CSS style string. 2. **Test Case 2 ("2")**: This test case uses the `Object.entries()` method to get an array of key-value pairs from the `props` object, maps each pair to a string in the format "key:value", and joins them together using the `join()` method. 3. **Test Case 3 ("3")**: This test case is similar to Test Case 2, but uses a separate variable `ents` to store the result of `Object.entries(props)`. 4. **Test Case 4 ("setAttribute")**: This test case uses the `el.setAttribute()` method directly to set the CSS style string. **Options Compared:** The benchmark compares the performance of four different approaches: 1. Traditional `for...in` loop 2. `Object.entries()` and `join()` 3. Separate variable to store result of `Object.entries(props)` 4. Direct use of `el.setAttribute()` method **Pros and Cons:** Here are some pros and cons for each approach: * **Traditional `for...in` loop**: Simple and straightforward, but may be slower due to the overhead of iterating over the object's properties. * **`Object.entries()` and `join()`:** Fast and efficient, as it uses a native method to get an array of key-value pairs. However, may require additional memory allocation. * **Separate variable to store result of `Object.entries(props)`:** Similar to the previous approach, but with an extra step that might add overhead. * **Direct use of `el.setAttribute()` method**: Simple and efficient, as it avoids the need for string manipulation. **Library Used:** None. This benchmark does not rely on any external libraries or frameworks. **Special JS Feature/Syntax:** The benchmark uses some JavaScript features and syntax, such as: * Template literals (`${k}:${props[k]};`) * `Object.entries()` * `join()` These are standard JavaScript features and do not require special knowledge to understand.
Related benchmarks:
test dwqdwqdwq
js css - style.cssText vs style vs setAttribute
getComputedStyle vs clientWidth
style.setProperty vs style.cssText vs style vs attributeStyleMap.set 3
Comments
Confirm delete:
Do you really want to delete benchmark?