0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

重たいコンポーネントとv-showの勘所

Posted at

最近あった出来事が印象的だったのでまとめます。
例えば以下のようなコンポーネントが定義されていたとします。SuperOmoiComponent はマウントするのに2秒かかります。membersの件数は、1000個の要素が存在しています。さて、SuperOmoiComponentがマウントして画面が自由になるのはどれぐらいかかるでしょうか。

<template>
  <div>
    <template v-for="(member, index)in members">
      <SuperOmoiComponent v-show="index < 10" :member="member"/>
    </template>
  </div>
</template>

...

ここで気づいてほしいのが、v-show のディレクティブです。v-show は、display: none を対象の要素やコンポーネントに設定するのですが、v-show を指定している場合はとりあえずレンダリングをする、という動きがなされます。SuperOmoiComponent はマウントに2秒かかるので、単純計算では 2 * 1000 秒かかることになります。一方で v-if を指定していた場合は、最大でも10件までのレンダリングしか行いませんので、 2 * 10 秒しかかかりません。

ということで、v-if / v-show も使い所を考えて、重いコンポーネントを大量に表示させるような場合には、v-show は使わないなどの工夫が必要なのだと改めて思った次第でした。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?