Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Multiple Attributes II
(version: 0)
Comparing performance of:
Default vs each with setAttribute vs setAttribute with loop vs Clone
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://d3js.org/d3.v5.min.js"></script>
Tests:
Default
const data = d3.range(100).map(() => ({ x: Math.random() * 300, y: Math.random() * 150 })); const svg = d3.select("body") .append("svg"); const circles = svg.selectAll(null) .data(data) .enter() .append("circle") .attr("cx", d=>d.x) .attr("cy", d=>d.y) .attr("r", 2) .style("fill", "teal");
each with setAttribute
const data = d3.range(100).map(() => ({ x: Math.random() * 300, y: Math.random() * 150 })); const svg = d3.select("body") .append("svg"); const circles = svg.selectAll(null) .data(data) .enter() .append("circle") .each((d, i, n) => { n[i].setAttribute("cx", d.x); n[i].setAttribute("cy", d.y); n[i].setAttribute("r", 2); }) .style("fill", "teal")
setAttribute with loop
const data = d3.range(100).map(() => ({ cx: Math.random() * 300, cy: Math.random() * 150, r: 2 })); const svg = d3.select("body") .append("svg"); const circles = svg.selectAll(null) .data(data) .enter() .append("circle") .each((d, i, n) => { for(let key in d){ n[i].setAttribute(key, d[key]) } }) .style("fill", "teal")
Clone
const data = d3.range(100).map(() => ({ cx: Math.random() * 300, cy: Math.random() * 150, r: 2 })); const svg = d3.select("body") .append("svg"); const detachedCircle = d3.create('svg:circle'); const circles = svg.selectAll(null) .data(data) .enter() .append(d => detachedCircle.clone() .attr("cx", d.x) .attr("cy", d.y) .attr("r", 2) .style("fill", "teal") .node());
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Default
each with setAttribute
setAttribute with loop
Clone
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):
I'll break down the provided benchmark and explain what's being tested, compared options, pros and cons of each approach, and other considerations. **Benchmark Overview** The MeasureThat.net website provides JavaScript microbenchmarks, allowing users to compare the performance of different approaches. The "Multiple Attributes II" benchmark is designed to test how browsers handle setting attributes on elements. **Test Cases** There are four test cases: 1. **Default**: This test case uses the `d3.select()` method with a simple string comparison (`d=>d.x`) to set the `cx` attribute. 2. **each with setAttribute**: In this test case, the `d3.select()` method is used with an array index and a function that sets attributes using `setAttribute()`. The loop iterates over each object in the array, setting all keys to their corresponding values. 3. **setAttribute with loop**: This test case uses `d3.create('svg:circle')` to create detached elements, which are then cloned and modified using `setattr()` with a loop. 4. **Clone**: As shown in the latest benchmark result, this test case creates detached circles using `d3.create('svg:circle')`, clones them, sets attributes, and styles them. **Comparison of Options** The main differences between these approaches are: * **Performance**: The "Default" and "each with setAttribute" methods seem to have similar performance profiles. However, the latter has slightly worse performance in this benchmark. * **Memory Usage**: The "setAttribute with loop" method may consume more memory due to the creation of detached elements and cloning. **Pros and Cons of Each Approach** 1. **Default**: * Pros: Simple, straightforward, and likely to be the most efficient way to set attributes. * Cons: May not account for complex attribute names or multiple iterations. 2. **each with setAttribute**: * Pros: Allows for more flexibility in setting attributes using a loop. * Cons: Has slightly worse performance and may consume more memory. 3. **setAttribute with loop**: * Pros: Can be useful when working with objects that have varying attribute names. * Cons: Creates detached elements, which can lead to increased memory usage and slower performance. 4. **Clone**: * Pros: Allows for efficient reuse of cloned elements without modifying the original DOM. * Cons: Requires more memory due to the creation and cloning of elements. **Other Considerations** * The use of `d3.select()` and `d3.create('svg:circle')` suggests that this benchmark is designed to test browser performance in a specific context, which may not be representative of general web development scenarios. * The presence of different attribute names (e.g., `cx`, `cy`) highlights the importance of considering varying attribute names when setting attributes. * The fact that some browsers perform worse than others on certain approaches indicates the need for browser-specific testing and optimization. **Alternatives** Other alternatives to consider might include: * Using `setAttribute()` with a single string comparison, similar to the "Default" test case, but accounting for complex attribute names or multiple iterations. * Exploring other libraries or frameworks that provide more efficient ways of setting attributes, such as React or Angular's DOM manipulation APIs.
Related benchmarks:
CircleSmallTest
cdfersd
Modern dataset vs old .getAttribute() vs jQuery .data() vs jQuery .attr() vs DOM node variable
Test Performance lodash
lodash vs es6 test
Comments
Confirm delete:
Do you really want to delete benchmark?