<svg className="container" xmlns="http://www.w3.org/2000/svg" style="position: absolute; width: 0; height: 0" width="0" height="0">
<defs>
<symbol id="umbrella" viewBox="0 0 32 32">
<path d="M0 48C0 21.5 21.5 0 48 0H176c26.5 0 48 21.5 48 48V176c0 26.5-21.5 48-48 48H48c-26.5 0-48-21.5-48-48V48zm288 0c0-26.5 21.5-48 48-48H464c26.5 0 48 21.5 48 48V176c0 26.5-21.5 48-48 48H336c-26.5 0-48-21.5-48-48V48zm48 240H464c26.5 0 48 21.5 48 48V464c0 26.5-21.5 48-48 48H336c-26.5 0-48-21.5-48-48V336c0-26.5 21.5-48 48-48zM0 336c0-26.5 21.5-48 48-48H176c26.5 0 48 21.5 48 48V464c0 26.5-21.5 48-48 48H48c-26.5 0-48-21.5-48-48V336zM416 64c0-8.8-7.2-16-16-16s-16 7.2-16 16V96H352c-8.8 0-16 7.2-16 16s7.2 16 16 16h32v32c0 8.8 7.2 16 16 16s16-7.2 16-16V128h32c8.8 0 16-7.2 16-16s-7.2-16-16-16H416V64zM64 96c-8.8 0-16 7.2-16 16s7.2 16 16 16h96c8.8 0 16-7.2 16-16s-7.2-16-16-16H64zM83.3 348.7c-6.2-6.2-16.4-6.2-22.6 0s-6.2 16.4 0 22.6L89.4 400 60.7 428.7c-6.2 6.2-6.2 16.4 0 22.6s16.4 6.2 22.6 0L112 422.6l28.7 28.7c6.2 6.2 16.4 6.2 22.6 0s6.2-16.4 0-22.6L134.6 400l28.7-28.7c6.2-6.2 6.2-16.4 0-22.6s-16.4-6.2-22.6 0L112 377.4 83.3 348.7zM352 352c-8.8 0-16 7.2-16 16s7.2 16 16 16h96c8.8 0 16-7.2 16-16s-7.2-16-16-16H352zm0 64c-8.8 0-16 7.2-16 16s7.2 16 16 16h96c8.8 0 16-7.2 16-16s-7.2-16-16-16H352z"></path>
</symbol>
</defs>
</svg>
<script src="https://unpkg.com/react@16/umd/react.production.min.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js" crossorigin></script>
<div id="main"></div>
var SVGInline = function() {
return React.createElement(
"svg",
{ className: "office", xmlns: "http://www.w3.org/2000/svg", width: 190, height: 190, viewBox: "0 0 188.5 188.5",
dangerouslySetInnerHTML: {
__html: '<path d="M0 48C0 21.5 21.5 0 48 0H176c26.5 0 48 21.5 48 48V176c0 26.5-21.5 48-48 48H48c-26.5 0-48-21.5-48-48V48zm288 0c0-26.5 21.5-48 48-48H464c26.5 0 48 21.5 48 48V176c0 26.5-21.5 48-48 48H336c-26.5 0-48-21.5-48-48V48zm48 240H464c26.5 0 48 21.5 48 48V464c0 26.5-21.5 48-48 48H336c-26.5 0-48-21.5-48-48V336c0-26.5 21.5-48 48-48zM0 336c0-26.5 21.5-48 48-48H176c26.5 0 48 21.5 48 48V464c0 26.5-21.5 48-48 48H48c-26.5 0-48-21.5-48-48V336zM416 64c0-8.8-7.2-16-16-16s-16 7.2-16 16V96H352c-8.8 0-16 7.2-16 16s7.2 16 16 16h32v32c0 8.8 7.2 16 16 16s16-7.2 16-16V128h32c8.8 0 16-7.2 16-16s-7.2-16-16-16H416V64zM64 96c-8.8 0-16 7.2-16 16s7.2 16 16 16h96c8.8 0 16-7.2 16-16s-7.2-16-16-16H64zM83.3 348.7c-6.2-6.2-16.4-6.2-22.6 0s-6.2 16.4 0 22.6L89.4 400 60.7 428.7c-6.2 6.2-6.2 16.4 0 22.6s16.4 6.2 22.6 0L112 422.6l28.7 28.7c6.2 6.2 16.4 6.2 22.6 0s6.2-16.4 0-22.6L134.6 400l28.7-28.7c6.2-6.2 6.2-16.4 0-22.6s-16.4-6.2-22.6 0L112 377.4 83.3 348.7zM352 352c-8.8 0-16 7.2-16 16s7.2 16 16 16h96c8.8 0 16-7.2 16-16s-7.2-16-16-16H352zm0 64c-8.8 0-16 7.2-16 16s7.2 16 16 16h96c8.8 0 16-7.2 16-16s-7.2-16-16-16H352z"></path>'
}
},
);
}
var DivTest = function() {
return React.createElement(
"div",
{ className: "office", width: 190, height: 190, },
"Foo"
);
}
ReactDOM.render(React.createElement(SVGInline, null), document.querySelector("#main"));
ReactDOM.render(React.createElement(DivTest, null), document.querySelector("#main"));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
inline svg | |
div |
Test name | Executions per second |
---|---|
inline svg | 517508.7 Ops/sec |
div | 562069.9 Ops/sec |
The benchmark provided aims to compare the performance of rendering an Inline SVG element versus a standard div
element using React. Below, I will break down the details, explain the comparisons made in the benchmarks, pros and cons of each approach, and alternatives that could be considered.
Inline SVG:
SVGInline
component defined in the preparation code, which leverages the React.createElement
method for a svg
tag along with dangerously setting inner HTML for the SVG path data.Div Element:
DivTest
component, which creates a simple div
containing the text "Foo."div
can be rendered.Based on the latest benchmark results, the div
rendered faster than the Inline SVG:
div
: 562,069.875Pros:
Cons:
Pros:
div
is a standard HTML element, and rendering it is typically faster due to lower complexity in comparison to SVG.Cons:
div
elements cannot render graphics natively. For vector-based or scalable graphics, additional libraries or CSS properties would be necessary.div
should heavily depend on the specific use case. For applications that heavily rely on graphics (like data visualization or interactive graphics), SVG is preferable despite its performance costs.Canvas API: For scenarios requiring fast drawing of graphics (such as games or high-frequency updates), the HTML5 <canvas>
element might be a better option. It allows for pixel manipulation but is not as accessible for scaling or responsive designs as SVG.
WebGL: For high-performance graphics applications, WebGL provides a way to render 3D graphics and complex 2D graphics directly in the browser leveraging GPU acceleration.
CSS Shapes and Animations: For simple shapes and animations, CSS can be used effectively with minimal performance overhead. CSS offers transitions and animations which are hardware-accelerated for smooth rendering.
React Libraries: There are several libraries, like D3.js or React Spring, that provide more robust ways of handling graphics within React applications, making it easier to integrate complex visual components without directly managing the DOM or SVG elements.
In conclusion, while Inline SVG is more capable for graphics-intensive applications, the div
element proves to be more performant for simpler tasks. The decision should be based on specific project needs, size of the graphics, and desired performance attributes.