# When/how to use setVerticesData vs. updateVerticesData

**URL:** <https://forum.babylonjs.com/t/when-how-to-use-setverticesdata-vs-updateverticesdata/41956>\
**Category:** Questions\
**Created:** [June 26, 2023, 6:42pm UTC](https://forum.babylonjs.com/t/when-how-to-use-setverticesdata-vs-updateverticesdata/41956 "2023-06-26T18:42:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kloklike](https://avatars.discourse-cdn.com/v4/letter/k/278dde/32.png) [@kloklike](https://forum.babylonjs.com/u/kloklike)\
**Post date:** [June 26, 2023, 6:42pm UTC](https://forum.babylonjs.com/t/when-how-to-use-setverticesdata-vs-updateverticesdata/41956/1 "2023-06-26T18:42:33Z")

</div>

Hello,  
It’s not clear to me from the docs when I should use mesh.setVerticesData vs. mesh.updateVerticesData. I have a tessellated plane mesh I’m changing the vertex positions of, and I was using updateVerticesData. Then I made another version and I accidentally used setVerticesData. Everything still looked correct, so that made me wonder which of those functions should I use in various situations? Is one more performant? More or less memory usage?

---

<div class="post-metadata">

**Author:** ![JCPalmer](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jcpalmer/32/42_2.png) [@JCPalmer](https://forum.babylonjs.com/u/JCPalmer)\
**Post date:** [June 26, 2023, 6:57pm UTC](https://forum.babylonjs.com/t/when-how-to-use-setverticesdata-vs-updateverticesdata/41956/2 "2023-06-26T18:57:27Z")

</div>

You must use `setVerticesData()` the first time (probably not a surprise). I have only been able to use `updateVerticesData()` afterward, and when the size of the data has not changed. I imagine update being better as long as it is possible.

One thing I have not tried is using replacing with something smaller, but I have a bad feeling about that.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [June 26, 2023, 9:11pm UTC](https://forum.babylonjs.com/t/when-how-to-use-setverticesdata-vs-updateverticesdata/41956/3 "2023-06-26T21:11:42Z")

</div>

`updateVerticesData` is better for perf when you can use it, see my answer here:

> [@Correct/performant way to build my own mesh/geometry?](https://forum.babylonjs.com/t/correct-performant-way-to-build-my-own-mesh-geometry/18782/12):
>
> mesh.setVerticesData creates a new VertexBuffer each time it is called. So, if you have to update data each frame, it’s better to use mesh.updateVerticesDataDirectly instead. Are you calling mesh.updateVerticesData with a true value as the 3rd parameter? If you do, you should pass false, or use mesh.updateVerticesDataDirectly instead, which never updates the bounding box and which executes a little less code than updateVerticesData. The bounding box is still created/set at creation time: doN…

---

<div class="post-metadata">

**Author:** ![kloklike](https://avatars.discourse-cdn.com/v4/letter/k/278dde/32.png) [@kloklike](https://forum.babylonjs.com/u/kloklike)\
**Post date:** [June 26, 2023, 11:14pm UTC](https://forum.babylonjs.com/t/when-how-to-use-setverticesdata-vs-updateverticesdata/41956/4 "2023-06-26T23:14:26Z")

</div>

Thank you.

One more question regarding the “updatable” aspect of a mesh’s geometry. I don’t need to update my vertex positions every frame, just every few seconds (a few hundred frames). During those few seconds of nothing happening, is there a performance penalty for having updatable == true? In other words, is it better to call setVerticesData with updatable false, even though it makes a new VertexBuffer, or is it better to call updateVerticesData which doesn’t seem to have an updatable option and presumably leaves updatable == true? I guess what I’m asking is, what does updatable really mean in terms of performance on an every-frame basis, and how does that compare to remaking a VertexBuffer every few seconds?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [June 27, 2023, 8:10am UTC](https://forum.babylonjs.com/t/when-how-to-use-setverticesdata-vs-updateverticesdata/41956/5 "2023-06-27T08:10:58Z")

</div>

cc @sebavan, I’m not sure there’s a big perf hit to use updatable vs non-updatable buffers, if any. So, if the buffers must be updated, even infrequently, I think it’s better to create them as updatable from start.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [June 27, 2023, 2:20pm UTC](https://forum.babylonjs.com/t/when-how-to-use-setverticesdata-vs-updateverticesdata/41956/6 "2023-06-27T14:20:39Z")

</div>

I do think that in most cases updatable won’t harm 🙂

---

<div class="post-metadata">

**Author:** ![JCPalmer](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jcpalmer/32/42_2.png) [@JCPalmer](https://forum.babylonjs.com/u/JCPalmer)\
**Post date:** [June 27, 2023, 2:49pm UTC](https://forum.babylonjs.com/t/when-how-to-use-setverticesdata-vs-updateverticesdata/41956/7 "2023-06-27T14:49:59Z")

</div>

Is there such a thing anymore of “pinned memory”, which can be directly accessed by both cpu & gpu?

Think the `updateable` arg maybe an indicator to use that memory when true. I make sure I only set it to true when mesh has morphing data / shapekeys, which will always be the same # of tris. No sense in wasting it on static geo.

Will probably continue to do this, but not sure it does anything.
