te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Vue.JS 2.0 slow when modifying unrelated data - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue.JS 2.0 slow when modifying unrelated data - Stack Overflow

programmeradmin3浏览0评论

Suppose I have an input field in Vue.JS that v-model bind to a String data property, and a long list of random numbers that are pletely unrelated to that first String.

data: {
  input: "",
  randoms: []
}

<input type="text" v-model="input">
<p v-for="random in randoms" v-text="random"></p>

When I put both in the same Vue, I see a huge slowdown when typing in the input field, as it appears Vue is reevaluating the DOM for each list entry after every input event, although they really have nothing to do with each other.

/

When I however move the v-for to a child ponent where I bind randoms to a prop, I experience no such slowdown

/

Is there a way I can achieve the performance of the second fiddle without using a child-ponent?

Suppose I have an input field in Vue.JS that v-model bind to a String data property, and a long list of random numbers that are pletely unrelated to that first String.

data: {
  input: "",
  randoms: []
}

<input type="text" v-model="input">
<p v-for="random in randoms" v-text="random"></p>

When I put both in the same Vue, I see a huge slowdown when typing in the input field, as it appears Vue is reevaluating the DOM for each list entry after every input event, although they really have nothing to do with each other.

https://jsfiddle/5jf3fmb8/2/

When I however move the v-for to a child ponent where I bind randoms to a prop, I experience no such slowdown

https://jsfiddle/j601cja8/1/

Is there a way I can achieve the performance of the second fiddle without using a child-ponent?

Share Improve this question asked Dec 8, 2016 at 15:08 Maximilian SchierMaximilian Schier 1,68915 silver badges20 bronze badges 1
  • You could use debouncing, causing the input variable to be updated less often. Updated fiddle: jsfiddle/asemahle/5jf3fmb8/3 – asemahle Commented Dec 8, 2016 at 16:07
Add a ment  | 

2 Answers 2

Reset to default 12

Is there a way I can achieve the performance of the second fiddle without using a child-ponent?

Short answer

No.

Long answer

Whenever any dependency of the template changes, Vue has to re-run the render function for the entire ponent and diff the new virtualDOM against the new one. It can't do this for this or that part of the template only, and skip the rest. Therefore, each time the input value changes, the entire virutalDOM is re-rendered.

Since your v-for is producing quite a bit of elements, this can take a few 100ms, enough to be noticable when you type.

Extracting the heavy part of the template into its own ponent is in fact the "official" way to optimize that.

As Alex explained, v-model.lazy might improve the situation a bit, but does not fix the core of the issue.

Shortest, simplest answer: change v-model to v-model.lazy.

When I put both in the same Vue, I see a huge slowdown when typing in the input field, as it appears Vue is reevaluating the DOM for each list entry after every input event, although they really have nothing to do with each other.

Note that the OnceFor sample still chugs like mad despite not actually being reactive any more. I don't understand Vue well enough to say if that's intentional or not.

const Example = {
  data() { return { input: "", randoms: [] } },
  created() { this.newRandoms() },
  methods: {
    newRandoms() { this.randoms = Array(50000).fill().map(() => Math.random()) }
  }
}

new Vue({
  el: "#vue-root",
  data(){ return {example: 'lazy-model'}},
  ponents: {
    LazyModel: {...Example, template: "#lazy-model"
    },
    OnceFor: {...Example, template: "#once-for"
    },
    InlineTemplate: {...Example, template: "#inline-template",
        ponents: {
          Welp: {
            props: ['randoms']
          }
        }
    }
  }
})
button,
input,
div {
  margin: 2px;
}
<script src="https://unpkg./vue/dist/vue.js"></script>

<div id="vue-root">
  <span><button v-for="(ponent, name) in $options.ponents" @click="$set($data, 'example', name)">{{name}}</button></span>
  <ponent :is="example"></ponent>
</div>

<template id="lazy-model">
<div>
  <input type="text" v-model.lazy="input"><br>
  <input type="submit" value="Regenerate" @click="newRandoms">
  <p v-for="random of randoms" v-text="random"></p>
</div>
</template>

<template id="once-for">
<div>
  <input type="text" v-model="input"><br>
  <input type="submit" value="Regenerate" @click="newRandoms">
  <p v-for="random of randoms" v-text="random" v-once></p>
</div>
</template>

<template id="inline-template">
<div>
  <input type="text" v-model="input"><br>
  <input type="submit" value="Regenerate" @click="newRandoms">
  <welp :randoms="randoms" inline-template>
    <div>
      <p v-for="(random, index) of randoms" :key="index"> {{index}}: {{random}} </p>
    </div>
  </welp>
</div>
</template>

发布评论

评论列表(0)

  1. 暂无评论