<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
// Benchmarks
updateVueState()
// Test React
updateReactState()
const { reactive } = Vue
// Création d'un état réactif
let state = reactive({ count: 0 })
// Fonction de test Vue.js
function updateVueState() {
for (let i = 0; i < 1000; i++) {
state.count += 1
}
}
// Simulation React
let state = { count: 0 }
// Fonction de test React
function updateReactState() {
for (let i = 0; i < 1000; i++) {
state = { state, count: state.count + 1 }
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Vue | |
React |
Test name | Executions per second |
---|---|
Vue | 1011619.3 Ops/sec |
React | 209546112.0 Ops/sec |
The provided benchmark compares the performance of state updates in two popular JavaScript frameworks: Vue.js and React. Specifically, it evaluates how each framework handles reactivity and state management through a series of operations aimed at incrementing a counter.
Vue.js Implementation:
reactive
function to create a reactive state object. updateVueState
function increments the count property of the reactive state within a loop 1,000 times. This makes use of Vue's reactive reactivity system to ensure that changes to the state are automatically detected and managed.const { reactive } = Vue;
let state = reactive({ count: 0 });
function updateVueState() {
for (let i = 0; i < 1000; i++) {
state.count += 1;
}
}
React Implementation:
updateReactState
function also increments the count, but it does so by creating a new object every time (using the spread operator ...
), which reflects how React manages state in functional components, leveraging immutability.let state = { count: 0 };
function updateReactState() {
for (let i = 0; i < 1000; i++) {
state = { ...state, count: state.count + 1 };
}
}
From the benchmark results:
Reactivity Approaches:
Vue:
React:
When evaluating frameworks for state management beyond Vue and React, consider the following alternatives:
Svelte:
Angular:
MobX or Redux:
The benchmark effectively highlights key performance differences between JavaScript's two predominant UI frameworks—Vue and React—specifically around state management and reactivity. Each framework has its strengths and weaknesses, and the choice often comes down to the specific needs of the project, the developer's familiarity with the framework, and the goals set for performance and maintainability.