我正在使用Redux和React来构建Web应用程序。我的应用程序是一个分析应用程序,可以呈现大量数据。当我的商店变大时,我遇到了性能问题。
在redux中避免大数据性能问题的首选方法是什么?
我的应用程序的结构方式是settings
(小json),results
(大json)和calculate(settings)
。计算是确定性的和幂等的。我的应用程序以图形,表格等方式呈现设置元数据和结果。
因为计算是确定性的和幂等的,所以我知道当设置没有改变时,结果不会改变。当设置更改时,我的结果可能会更改。换句话说,我很乐意仅依靠设置来确定是否需要更新我的UI。
当我在商店中包含结果时,我的申请速度很慢:
let settings = { foo: "bar" };
let results = calculate(settings);
Object.assign({}, state, { settings: settings, results: results });
//deterministic and idempotent
function calculate(settings) {
return /*large json*/;
}
当我参考时,我的申请很快,但我的商店里面没有包含结果:
let settings = { foo: "bar" };
let results = calculate(settings);
Object.assign({}, state, { settings: settings, results: () => results });
//deterministic and idempotent
function calculate(settings) {
return /*large json*/;
}
我知道redux建议不要存储函数,所以另一种方法是使用字符串保存引用......如:
let settings = { foo: "bar" };
let calculationId = uuid(); //could also be a hash of settings if I want to avoid some false positive UI re-rendering
let results = calculate(settings);
setResults(calculationId, results);
Object.assign({}, state, { settings: settings, calculationId: calculationId });
//deterministic and idempotent
function calculate(settings) {
return /*large json*/;
}
let fakeStore = { calculationId: null, results: null };
function setResults(calculationId, results) {
fakeStore. calculationId = calculationId;
fakeStore.results = results;
}
function getResults(calculationId) {
if(fakeStore.calculationId === calculationId) {
return fakeStore.results;
}
throw new Error("Invalid data access");
}
render() {
let calculationId = this.props.calculationId;
let results = getResults(calculationId);
}
将大数据保存在商店之外是可行的,但似乎有更好的方法可以支持这个用例。
使用redux 3.7.1,反应15.4.2,react-redux 4.4.8
答案 0 :(得分:1)
简单地将价值保存到商店"不会影响速度。它是你如何处理你的reducer中的那些值,并在你的UI中使用它们。因此,提供具体建议有点困难,因为您还没有提供有关您如何实际使用该数据的任何信息。
通常,根据其他注释,使用memoized选择器(由Reselect库创建)有助于仅在输入实际更改时重新计算昂贵的派生值。
有关提高Redux使用性能的更多信息,请参阅:
答案 1 :(得分:0)
试试Reselect。每次计算后,结果对象引用是否会更改?如果是这样的话,这个小型图书馆可以帮助你。