<script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
<div id="app1"></div>
<div id="app2"></div>
var cards = function () {
return new Array(28).fill(null).map(function (card) {
return new Array(4).fill(null).map(function (_, index) {
return "azaza".concat(index);
});
});
}();
console.log(cards)
var FragmentCard = function FragmentCard(props) {
return React.createElement(React.Fragment, null, props.words.map(function (word) {
return React.createElement(React.Fragment, null, "word", React.createElement("div", null));
}));
};
var FragmentCards = function FragmentCards(props) {
return props.cards.map(function (words) {
return React.createElement(FragmentCard, {
words: words
});
});
};
var ArrayCard = function ArrayCard(props) {
return React.createElement(React.Fragment, null, props.words.reduce(function (result, feature, index, _ref) {
var length = _ref.length;
result.push(feature, index < length - 1 && React.createElement("div", {
key: "s".concat(index)
}));
return result;
}, []));
};
var ArrayCards = function ArrayCards(props) {
return props.cards.map(function (words) {
return React.createElement(ArrayCard, {
words: words
});
});
};
var app1 = document.getElementById('app1');
var app2 = document.getElementById('app2');
var a = React.createElement(FragmentCards, { cards })
ReactDOM.render(a, app1)
var a = React.createElement(ArrayCards, { cards })
ReactDOM.render(a, app2)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Fragment | |
Array |
Test name | Executions per second |
---|---|
Fragment | 13897.0 Ops/sec |
Array | 23261.6 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
The benchmark in question is comparing two approaches to rendering React components: React.Fragment
and an array-based approach, where each component is rendered as a separate element in an array.
What are we testing?
We're measuring the execution time and number of executions per second for each approach. The benchmark tests how efficiently React can render these components on a desktop machine with Chrome 121 browser.
Options compared:
Pros and Cons:
Library and purpose:
The benchmark uses the React library to render components. Specifically, it utilizes the React.createElement
function to create React elements and the ReactDOM.render
function to render them on the DOM.
Special JS feature or syntax:
There are no special JavaScript features or syntax used in this benchmark that aren't already standard in JavaScript.
Other alternatives:
For those interested in exploring alternative approaches, there are other ways to optimize React rendering. Some examples include:
In conclusion, this benchmark provides a simple yet insightful way to compare the performance of two React rendering approaches: React.Fragment
and an array-based approach. By understanding the pros and cons of each method, developers can make informed decisions about how to optimize their React applications for better performance.