简体   繁体   English

如果不在开关盒内声明块作用域,是否会导致内存泄漏? (ESLint无大小写声明)

[英]Does it cause a memory leak if you do not declare block scope inside a switch case? (ESLint no-case-declarations)

Take this code for example; 以下面的代码为例; it is a Redux reducer function: 它是Redux的reducer功能:

export default (state = initialState, action) => {
    switch (action.type) {
        case EMPLOYEE_UPDATE: {                                           // <-- this {
            // action.payload === { prop: 'name', value: 'Jane' }
            const { prop, value } = action.payload
            return { ...state, [prop]: value }
        }                                                                 // <-- and this }

        default:
            return state
    }
}

I was simply trying to destructure action.payload to minimize duplication, and I noticed it was aggravating the ES Lint rule ( no-case-declarations ). 我只是在尝试对action.payload进行重组以最大程度地减少重复,并且我注意到它加剧了ES Lint规则( 无大小写声明 )。 Normally, I might turn it off after confirming the rule. 通常,我可能会在确认规则后将其关闭。 This one seems much more serious due to this ES Lint definition: 由于ES Lint的定义,这一点似乎更为严重:

...The reason is that the lexical declaration is visible in the entire switch block but it only gets initialized when it is assigned, which will only happen if the case where it is defined is reached. ...原因是词法声明在整个switch块中都是可见的,但仅在分配时才被初始化,只有在达到定义的情况下才会发生。

Source: https://eslint.org/docs/rules/no-case-declarations 资料来源: https : //eslint.org/docs/rules/no-case-declarations

If I remember memory leak potential correctly, does this mean the compiler will maintain a reference to action.payload at all times? 如果我没有记错内存泄漏的潜在可能性,这是否意味着编译器将始终保持对action.payload的引用? -- meaning that if a large loop, dataset or long running calculation got into there, it could cause massive memory consumption even though the case only executes when it matches because it cannot be garbage collected or ignored ? -意味着如果进入了大循环,数据集或长时间运行的计算,即使这种情况仅在匹配时才执行, 因为它不能被垃圾收集或忽略 ,它也可能导致大量的内存消耗?

First and foremost, is my interpretation correct? 首先,我的解释正确吗?

The nature of my question is around what exactly { and } are protecting against in this execution context/lexical environment. 我的问题的本质是围绕{}在此执行上下文/词法环境中到底要防止什么。 Is it just memory leak potential or is there a topic I did not mention, as well? 仅仅是潜在的内存泄漏,还是我没有提到的话题?

The problem: 问题:

 switch (true) { case false: let x = 10; break; case true: let x = 20; //error there's already an x declaration in scope break; } 

The cases all share the same lexical scope. 这些案例都具有相同的词汇范围。 So the example runs an error. 因此,该示例运行错误。

So, a way to fix this is by adding a block statement to introduce block scope and localize the lexical declarations. 因此,解决此问题的一种方法是添加一个block语句以引入block范围并本地化词汇声明。

 switch (true) { case false: { let x = 10; break; } case true: { let x = 20; break; } } 

It's unrelated to memory issues. 它与内存问题无关。 Other than initialized bindings inside the switch block (that should eventually be GC). 除了switch块内的初始化绑定(最终应为GC)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM