首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(Vue)对计算属性中局部变量性能的影响

(Vue)对计算属性中局部变量性能的影响
EN

Stack Overflow用户
提问于 2018-09-12 16:06:41
回答 1查看 1.3K关注 0票数 3

定义计算属性中的变量对Vue组件的性能有任何影响吗?

背景:我构建了一个表组件,它一般从传递的数据生成HTML表,并且每个列都有不同的过滤器、整个表的筛选器、排序键等等,所以我在计算属性中定义了许多局部变量。

想象一下拥有一个对象数组:

代码语言:javascript
复制
let data = [
  { id: "y", a: 1, b: 2, c: 3 },
  { id: "z", a: 11, b: 22, c: 33 }
]

Vue组件使用..which来显示数据:

代码语言:javascript
复制
<template>
  <div>
    <input type="text" v-model="filterKey" />
  </div>

  <table>
    <thead>
      <tr>
        <th>A</th>
        <th>B</th>
        <th>C</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="item in filteredData" :key="item.id">
        <td v-for="(value, key) in item" :key="key">
          {{ value }}
        </td>
      </tr>
    </tbody>
  </table>
</template>

通过输入过滤数据:

代码语言:javascript
复制
<script>
export default {
  props: {
    passedData: Array,
  },
  data() {
    return {
      filterKey: null,
    };
  },
  computed: {
    filteredData() {
      // defining local scope variables
      let data = this.passedData;
      let filterKey = this.filterKey;

      data = data.filter((e) => {
        // filter by filterKey or this.filterKey
      });

      return data;
    },
  },
};
</script>

我的问题提到了let data = ..let filterKey = ..,因为filteredData()是由filterKey的任何更改(在data()中定义的)触发的,因此局部变量也会被更新,尽管它们在Vue方式上不是“反应性”的。

在计算属性中定义局部变量时,对性能有任何影响吗?您是否应该直接在计算属性中使用来自data() (例如,this.filterKey)的反应变量?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-12 16:31:19

测试某件事是否影响性能的最好方法是实际测试它。

根据下面的测试,使用this.passedData比在函数顶部添加变量慢1000%以上。(869 vs Vs 29 Vs)

确保在目标浏览器上运行基准测试,编写应用程序以获得最佳结果。

代码语言:javascript
复制
function time(name, cb) {
  var t0 = performance.now();
  const res = cb();
  if(res !== 20000000) {
    throw new Error('wrong result: ' + res);
  }
  var t1 = performance.now();
  document.write("Call to "+name+" took " + (t1 - t0) + " milliseconds.<br>")
}
function withoutLocalVar() {
  const vue = new Vue({
    computed: {
      hi() {
        return 1;
      },
      hi2() {
        return 1;
      },
      test() {
        let sum = 0;
        for(let i = 0; i < 10000000; i++) { // 10 000 000
          sum += this.hi + this.hi2;
        }
        return sum;
      },
    }
  })
  return vue.test;
}
function withLocalVar() {
  const vue = new Vue({
    computed: {
      hi() {
        return 1;
      },
      hi2() {
        return 1;
      },
      test() {
        let sum = 0;
        const hi = this.hi;
        const hi2 = this.hi2;
        for(let i = 0; i < 10000000; i++) { // 10 000 000
          sum += hi + hi2;
        }
        return sum;
      },
    }
  })
  return vue.test;
}
function benchmark() {
  const vue = new Vue({
    computed: {
      hi() {
        return 1;
      },
      hi2() {
        return 1;
      },
      test() {
        let sum = 0;
        const hi = 1;
        const hi2 = 1;
        for(let i = 0; i < 10000000; i++) { // 10 000 000
          sum += hi + hi2;
        }
        return sum;
      },
    }
  })
  return vue.test;
}
time('withoutLocalVar - init', withoutLocalVar);
time('withLocalVar - init', withLocalVar);
time('benchmark - init', benchmark);
time('withoutLocalVar - run1', withoutLocalVar);
time('withLocalVar - run1', withLocalVar);
time('benchmark - run1', benchmark);
time('withoutLocalVar - run2', withoutLocalVar);
time('withLocalVar - run2', withLocalVar);
time('benchmark - run2', benchmark);
代码语言:javascript
复制
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52299407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档