javascript - 拥有状态和使用函数获取状态之间的性能差异是什么?
问题描述
举个例子,让我们看看我们有三个事件侦听器,mousemove
, keydown
, keyup
。我们要记录space
键和当前鼠标位置。
import getState from "../state";
//version one, the variable is in place and gets mutated if changes
let spacePressed = false;
window.addEventListener("mousemove", (x, y) => {
if (spacePressed) doSomethingWithMousePos(x, y);
});
//version two, the variable is in a state outside
window.addEventListener("mousemove", (x, y) => {
const { spacePressed } = getState();
if (spacePressed) doSomethingWithMousePos(x, y);
});
//...
window.addEventListener("keydown", functionWhichSetsSpacePressed);
window.addEventListener("keyup", functionWhichSetsSpacePressed);
只是为了澄清它与 React 或 Redux 无关。这个例子很好,因为mousemove
经常发生,所以即使是很小的性能差异也很重要。那么,将状态放在适当的位置,函数只需要读取它,将状态放在外部文件中,函数在其中传递状态本身有什么区别?(像这样:
const state = {};
export const getState = () => state;
)
解决方案
首先要提到的是,在前端 javascript 中优化这些东西几乎没有用,因为这里的主要瓶颈是 DOM 操作和 DOM 渲染。但如果你真的有兴趣,我不会回答什么是更快,什么是慢,我会发布一种方法来轻松地使用benchmark.js
.
var suite = new Benchmark.Suite();
const state = {};
const getState = () => state;
// add tests
suite.add('Get State', function() {
getState()['foo'] = 'bar';
})
.add('Read state', function() {
state.foo = 'bar';
}).on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
}).run({'async': true});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js" ></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/2.1.4/benchmark.min.js" ></script>
</body>
</html>
推荐阅读
- branch.io - Branch.io 是否支持在横幅上显示动态的应用评论数量?
- python - 使用 BeautifulSoup 忽略列表中的某些元素
- jekyll - Jekyll Front Matter Path 不起作用
- python-3.x - aiohttp 如何为多个请求共享一个连接?
- wpf - mvvm light wpf 导航
- typescript - 根据同一个通用本地模块打包多个 Typescript 项目
- system-verilog - SystemVerilog $test$plusargs 匹配子字符串
- spring - 如何使用 spring boot 和 spock 运行测试容器
- android - 当我在 Flutter 中使用 appBar 时,Android 导航栏颜色从默认的白色(在 Oreo 中)变为黑色,为什么?
- clojure - InputStream 到 Clojure 环映射