javascript / expert
Snippet
Optimizing Performance with markRaw
When dealing with large, complex third-party library instances (like Three.js scenes or Mapbox maps), Vue's deep reactivity can cause massive overhead. 'markRaw' explicitly tells Vue's reactivity system to skip this object, preserving performance by avoiding unnecessary Proxy wrapping.
snippet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { markRaw, reactive } from 'vue';import { ThirdPartyEngine } from './external';export default {setup() {const state = reactive({// markRaw prevents Vue from converting the engine into a Proxyengine: markRaw(new ThirdPartyEngine()),active: false});return { state };}};
vue
Breakdown
1
engine: markRaw(new ThirdPartyEngine())
Wraps the instance so Vue ignores it for reactive tracking.
2
reactive({ ... })
The surrounding state remains reactive, but 'engine' stays a plain object.