performance - (Vue) 计算属性中局部作用域变量对性能的影响

标签 performance vue.js vuejs2 computed-properties

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

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

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

let data = [
  { id: "y", a: 1, b: 2, c: 3 },
  { id: "z", a: 11, b: 22, c: 33 }
]

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

<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>

数据通过输入过滤:

<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 方式的“响应式(Reactive)”。

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

最佳答案

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

根据我下面的测试,使用 this.passedData 而不是在函数顶部添加变量的一致性要慢 1000% 以上。 (869 毫秒对 29 毫秒)

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

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);
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>

关于performance - (Vue) 计算属性中局部作用域变量对性能的影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52299407/

相关文章:

vue.js - Vue Router - 重新加载页面后无法解析异步组件

javascript - vue,用另一个组件编辑组件的内容

css - 如何将 Chrome 开发人员工具检查器中的 CSS 更改保存到 .vue 文件

python - 查找python中较长字符串中是否存在短字符串的有效方法

parsing - PostgreSQL 对极短查询的解析异常缓慢

performance - OpenCL矩阵乘法应该更快?

performance - 1.6之后如何提升Magento网站?

vue.js - 生产和暂存构建不同

django - vue.js 和 django 中的访问控制允许来源问题

javascript - 视觉 : the template root disallows v-for directives