Skip to content

Commit 4003382

Browse files
frankcsktsn
authored andcommitted
perf: Implementing a cache for the gettersProxy object creation (#1546)
* Implementing a cache for the gettersProxy object creation. Kill ssr performance wth large number of modules/getters * Revert "Implementing a cache for the gettersProxy object creation. Kill ssr performance wth large number of modules/getters" This reverts commit 2df536b. * Resetting the make local getters cache when the store gets updated * Changing cache to makeLocalGetters to instance internal state
1 parent 540b81f commit 4003382

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

src/store.js

+23-18
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export class Store {
3535
this._modulesNamespaceMap = Object.create(null)
3636
this._subscribers = []
3737
this._watcherVM = new Vue()
38+
this._makeLocalGettersCache = Object.create(null)
3839

3940
// bind commit and dispatch to self
4041
const store = this
@@ -252,6 +253,8 @@ function resetStoreVM (store, state, hot) {
252253

253254
// bind store public getters
254255
store.getters = {}
256+
// reset local getters cache
257+
store._makeLocalGettersCache = Object.create(null)
255258
const wrappedGetters = store._wrappedGetters
256259
const computed = {}
257260
forEachValue(wrappedGetters, (fn, key) => {
@@ -397,26 +400,28 @@ function makeLocalContext (store, namespace, path) {
397400
}
398401

399402
function makeLocalGetters (store, namespace) {
400-
const gettersProxy = {}
401-
402-
const splitPos = namespace.length
403-
Object.keys(store.getters).forEach(type => {
404-
// skip if the target getter is not match this namespace
405-
if (type.slice(0, splitPos) !== namespace) return
406-
407-
// extract local getter type
408-
const localType = type.slice(splitPos)
409-
410-
// Add a port to the getters proxy.
411-
// Define as getter property because
412-
// we do not want to evaluate the getters in this time.
413-
Object.defineProperty(gettersProxy, localType, {
414-
get: () => store.getters[type],
415-
enumerable: true
403+
if (!store._makeLocalGettersCache[namespace]) {
404+
const gettersProxy = {}
405+
const splitPos = namespace.length
406+
Object.keys(store.getters).forEach(type => {
407+
// skip if the target getter is not match this namespace
408+
if (type.slice(0, splitPos) !== namespace) return
409+
410+
// extract local getter type
411+
const localType = type.slice(splitPos)
412+
413+
// Add a port to the getters proxy.
414+
// Define as getter property because
415+
// we do not want to evaluate the getters in this time.
416+
Object.defineProperty(gettersProxy, localType, {
417+
get: () => store.getters[type],
418+
enumerable: true
419+
})
416420
})
417-
})
421+
store._makeLocalGettersCache[namespace] = gettersProxy
422+
}
418423

419-
return gettersProxy
424+
return store._makeLocalGettersCache[namespace]
420425
}
421426

422427
function registerMutation (store, type, handler, local) {

0 commit comments

Comments
 (0)