简体   繁体   English

反应:道具分解和内存使用

[英]React: Props destructuring and memory usage

Does using destructuring assignments affect memory usage/performance if the declared constants are only used once ? 如果声明的常量仅使用一次,使用解构分配是否会影响内存使用/性能? In other languages (Java, C, etc), it's best not to declare a variable if it's only used once, as it allocates unnecessary memory . 在其他语言(Java,C等)中, 如果只使用一次,则最好不要声明变量,因为它会分配不必要的内存 Is this also true with ES6 JS? ES6 JS也是如此吗?

I have 2 examples of what I'm talking about. 我有2个我所谈论的例子。 The first uses destructuring assignments and calls each constant just once. 第一种使用解构分配,并且仅一次调用每个常量。 The second calls this.props each time it is used and does not declare them in advance. 第二个函数每次使用时都调用this.props,并且不提前声明它们。 Which one uses less memory? 哪一个使用的内存更少? Our code base is pretty evenly split between the 2, but I'm curious as to which way is better for scale. 我们的代码库在2个代码中几乎是平均分配的,但是我很好奇哪种方法更适合扩展。

with destructuring assignment for single-reference variables: 具有针对单引用变量的解构分配:

render(){
   const {
      a, b, c, d, e, f, g, h, i
   } = this.props;
   return(
      <div>
         <div id={a}>some text relevant to a</div>
         <div id={b}>some text relevant to b</div>
         <div id={c}>some text relevant to c</div>
         <div id={d}>some text relevant to d</div>
         <div id={e}>some text relevant to e</div>
         <div id={f}>some text relevant to f</div>
         <div id={g}>some text relevant to g</div>
         <div id={h}>some text relevant to h</div>
         <div id={i}>some text relevant to i</div>
      </div>
   );
}

without declaring constants for single-reference variables: 无需为单引用变量声明常量:

render(){
   return(
      <div>
         <div id={this.props.a}>some text relevant to a</div>
         <div id={this.props.b}>some text relevant to b</div>
         <div id={this.props.c}>some text relevant to c</div>
         <div id={this.props.d}>some text relevant to d</div>
         <div id={this.props.e}>some text relevant to e</div>
         <div id={this.props.f}>some text relevant to f</div>
         <div id={this.props.g}>some text relevant to g</div>
         <div id={this.props.h}>some text relevant to h</div>
         <div id={this.props.i}>some text relevant to i</div>
      </div>
   );
}

There is no difference between both code in terms of performance and memory usage. 在性能和内存使用方面,这两个代码之间没有区别。

It's more about code readability : your first solution makes the JSX less verbose and easier to read. 它更多地与代码的可读性有关 :您的第一个解决方案使JSX不再那么冗长,更易于阅读。 Especially if you reuse several time the same props. 特别是如果您多次重复使用相同的道具。

When you have a doubt about performance or memory usage, open the Google Chrome Developer Tools and start monitoring your app. 如果对性能或内存使用有疑问,请打开Goog​​le Chrome开发者工具并开始监视您的应用。 Don't micro-optimize before detecting a problem. 在检测到问题之前,请勿进行微优化。

I would argue with the first answer. 我会与第一个答案争论。 When doing 做的时候

const {
      a, b, c, d, e, f, g, h, i
} = this.props;

you allocate memory for these variables. 您为这些变量分配内存。 Although you'll need some extra memory, it wont be significant because objects (including arrays, functions etc) will be defined as references/pointers. 尽管您将需要一些额外的内存,但这并不重要,因为对象(包括数组,函数等)将被定义为引用/指针。

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

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