Skip to content

Commit

Permalink
Implementing a cache for the gettersProxy object creation. Kill ssr p…
Browse files Browse the repository at this point in the history
…erformance wth large number of modules/getters
  • Loading branch information
frankcs committed May 3, 2019
1 parent d7c7f98 commit 2df536b
Show file tree
Hide file tree
Showing 3 changed files with 16,864 additions and 40 deletions.
65 changes: 43 additions & 22 deletions dist/vuex.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ function applyMixin (Vue) {
}
}

var devtoolHook =
typeof window !== 'undefined' &&
window.__VUE_DEVTOOLS_GLOBAL_HOOK__;
var target = typeof window !== 'undefined'
? window
: typeof global !== 'undefined'
? global
: {};
var devtoolHook = target.__VUE_DEVTOOLS_GLOBAL_HOOK__;

function devtoolPlugin (store) {
if (!devtoolHook) { return }
Expand Down Expand Up @@ -89,6 +92,12 @@ function assert (condition, msg) {
if (!condition) { throw new Error(("[vuex] " + msg)) }
}

function partial (fn, arg) {
return function () {
return fn(arg)
}
}

// Base data struct for store's module, package with some attribute and method
var Module = function Module (rawModule, runtime) {
this.runtime = runtime;
Expand Down Expand Up @@ -550,7 +559,9 @@ function resetStoreVM (store, state, hot) {
var computed = {};
forEachValue(wrappedGetters, function (fn, key) {
// use computed to leverage its lazy-caching mechanism
computed[key] = function () { return fn(store); };
// direct inline function use will lead to closure preserving oldVm.
// using partial to return function with only arguments preserved in closure enviroment.
computed[key] = partial(fn, store);
Object.defineProperty(store.getters, key, {
get: function () { return store._vm[key]; },
enumerable: true // for local getters
Expand Down Expand Up @@ -687,27 +698,37 @@ function makeLocalContext (store, namespace, path) {
return local
}

var makeLocalGettersCache = {};
var cacheStore = {};

function makeLocalGetters (store, namespace) {
var gettersProxy = {};

var splitPos = namespace.length;
Object.keys(store.getters).forEach(function (type) {
// skip if the target getter is not match this namespace
if (type.slice(0, splitPos) !== namespace) { return }

// extract local getter type
var localType = type.slice(splitPos);

// Add a port to the getters proxy.
// Define as getter property because
// we do not want to evaluate the getters in this time.
Object.defineProperty(gettersProxy, localType, {
get: function () { return store.getters[type]; },
enumerable: true
if (cacheStore !== store) {
makeLocalGettersCache = {};
cacheStore = store;
}

if (!makeLocalGettersCache[namespace]) {
var gettersProxy = {};
var splitPos = namespace.length;
Object.keys(store.getters).forEach(function (type) {
// skip if the target getter is not match this namespace
if (type.slice(0, splitPos) !== namespace) { return }

// extract local getter type
var localType = type.slice(splitPos);

// Add a port to the getters proxy.
// Define as getter property because
// we do not want to evaluate the getters in this time.
Object.defineProperty(gettersProxy, localType, {
get: function () { return store.getters[type]; },
enumerable: true
});
});
});
makeLocalGettersCache[namespace] = gettersProxy;
}

return gettersProxy
return makeLocalGettersCache[namespace]
}

function registerMutation (store, type, handler, local) {
Expand Down
Loading

0 comments on commit 2df536b

Please sign in to comment.