<script src="https://unpkg.com/vue@3"></script>
const {reactive, computed} = Vue;
const v = reactive({
foo: "bar"
});
const bar = v.foo;
const frog = computed(()=>{
return v.foo + "!";
});
v.foo = "foo"
const {ref, computed} = Vue;
const foo = ref("bar");
const bar = foo.value;
const frog = computed(()=>{
return foo.value + "!";
});
foo.value = "foo"
const {shallowRef, computed} = Vue;
const foo = shallowRef("bar");
const bar = foo.value;
const frog = computed(()=>{
return foo.value + "!";
});
foo.value = "foo"
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Reactive | |
Ref | |
ShallowRef |
Test name | Executions per second |
---|---|
Reactive | 1062728.5 Ops/sec |
Ref | 8096441.0 Ops/sec |
ShallowRef | 8074187.5 Ops/sec |
Let's break down the provided JSON and benchmark code to understand what's being tested.
Benchmark Definition
The benchmark is comparing three approaches in Vue.js: reactive
, ref
, and shallowRef
. These are all used for managing state in Vue components, but they have different use cases and performance implications.
Options Compared
Here's a brief overview of each option:
ref
, but it only updates the top-level value of the shallow ref, whereas ref
can be updated recursively.Pros and Cons
Here are some pros and cons for each approach:
ref
, but with less overhead due to only updating the top-level value.Library and Purpose
None of these options rely on an external library. They are all built-in features of Vue.js.
Special JS Features or Syntax
None mentioned.
Benchmark Results
The benchmark results show the number of executions per second for each test case:
Ref
: 8823568.0 executions per secondShallowRef
: 8786086.0 executions per secondReactive
: 413783.84375 executions per secondIt appears that ref
is slightly faster than shallowRef
, while reactive
is slower.
Other Alternatives
If you're looking for alternative state management approaches in Vue.js, some options include:
Each of these alternatives has its own trade-offs and use cases, so it's worth exploring their documentation to determine which one best fits your project's needs.