# Gltf loader: do not clone animation keys

**URL:** <https://forum.babylonjs.com/t/gltf-loader-do-not-clone-animation-keys/60903>\
**Category:** Completed Features\
**Tags:** animation, performance, gltffileloader, memory\
**Created:** [October 11, 2025, 3:38am UTC](https://forum.babylonjs.com/t/gltf-loader-do-not-clone-animation-keys/60903 "2025-10-11T03:38:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kzhsw](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kzhsw](https://forum.babylonjs.com/u/kzhsw)\
**Post date:** [October 11, 2025, 3:38am UTC](https://forum.babylonjs.com/t/gltf-loader-do-not-clone-animation-keys/60903/1 "2025-10-11T03:38:12Z")

</div>

In glTFLoaderAnimation, animation is constructed with `setKeys()`

> <https://github.com/BabylonJS/Babylon.js/blob/c190296494a0c192edb26689045c929902476937/packages/dev/loaders/src/glTF/2.0/glTFLoaderAnimation.ts#L45>

And setKeys defaults to clone keys

> <https://github.com/BabylonJS/Babylon.js/blob/c190296494a0c192edb26689045c929902476937/packages/dev/core/src/Animations/animation.ts#L1219>

Since these keys are built in-place for each channel (should be for each sampler, since gltf samplers can be reused, and we can discuss it later), keys should not be cloned, to reduce gc pressure

---

<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:** [October 13, 2025, 3:14pm UTC](https://forum.babylonjs.com/t/gltf-loader-do-not-clone-animation-keys/60903/2 "2025-10-13T15:14:53Z")

</div>

cc @bghgary and @alexchuber but I guess a PR would be welcomed 🙂

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [October 13, 2025, 8:54pm UTC](https://forum.babylonjs.com/t/gltf-loader-do-not-clone-animation-keys/60903/3 "2025-10-13T20:54:51Z")

</div>

Seems reasonable. I don’t know how to test this though. How do we ensure this doesn’t break anything?

---

<div class="post-metadata">

**Author:** ![kzhsw](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kzhsw](https://forum.babylonjs.com/u/kzhsw)\
**Post date:** [October 25, 2025, 1:53am UTC](https://forum.babylonjs.com/t/gltf-loader-do-not-clone-animation-keys/60903/4 "2025-10-25T01:53:27Z")

</div>

Well, by static analysis. `keys` are created in callback in `_loadAnimationChannelFromTargetInfoAsync`, and passed to `propertyInfo.buildAnimations`, and never escaped from the scope.

> <https://github.com/BabylonJS/Babylon.js/blob/87b3b6a801aaf7eaaaf35ace0b2c1dfc135e8b3e/packages/dev/loaders/src/glTF/2.0/glTFLoader.ts#L1855>

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [October 27, 2025, 3:47pm UTC](https://forum.babylonjs.com/t/gltf-loader-do-not-clone-animation-keys/60903/5 "2025-10-27T15:47:37Z")

</div>

Ahh, sorry, I misunderstand what was happening, read the code too fast 🙂 Yes, this seems good. Did you want to create a PR?

---

<div class="post-metadata">

**Author:** ![kzhsw](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kzhsw](https://forum.babylonjs.com/u/kzhsw)\
**Post date:** [October 28, 2025, 2:11am UTC](https://forum.babylonjs.com/t/gltf-loader-do-not-clone-animation-keys/60903/6 "2025-10-28T02:11:10Z")

</div>

Yeah, here it is:

> <https://github.com/BabylonJS/Babylon.js/pull/17352>
>
> This commit skips animation keys cloning by setting \`dontClone=true\` when callin…g \`Animation.setKeys\`. This should reduce peak heap allocation and GC pressure when loading animation-heavy models.
> In GLTF loader, keys are created in callback in \`\_loadAnimationChannelFromTargetInfoAsync\`, and passed to \`propertyInfo.buildAnimations\`, and never escaped from the scope, so it should be safe to do this change.
> 
> See also the forum post \<https://forum.babylonjs.com/t/gltf-loader-do-not-clone-animation-keys/60903\>

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [October 28, 2025, 4:01pm UTC](https://forum.babylonjs.com/t/gltf-loader-do-not-clone-animation-keys/60903/7 "2025-10-28T16:01:29Z")

</div>



---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [October 28, 2025, 4:01pm UTC](https://forum.babylonjs.com/t/gltf-loader-do-not-clone-animation-keys/60903/8 "2025-10-28T16:01:52Z")

</div>

It is merged. Thanks for your contribution!
