# Best practices to use TObjectPtr, TWeakObjectPtr and Raw ptr

**URL:** <https://forums.unrealengine.com/t/best-practices-to-use-tobjectptr-tweakobjectptr-and-raw-ptr/2447987>\
**Category:** Debugging, Optimization, & Profiling\
**Tags:** unreal-engine, question, CPP\
**Created:** [April 9, 2025, 10:31am UTC](https://forums.unrealengine.com/t/best-practices-to-use-tobjectptr-tweakobjectptr-and-raw-ptr/2447987 "2025-04-09T10:31:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![krystian5642](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@krystian5642](https://forums.unrealengine.com/u/krystian5642)\
**Post date:** [April 9, 2025, 10:31am UTC](https://forums.unrealengine.com/t/best-practices-to-use-tobjectptr-tweakobjectptr-and-raw-ptr/2447987/1 "2025-04-09T10:31:20Z")

</div>

Hi, what is the best practice with `TObjectPtr` and `TWeakObjectPtr`? From what I have seen so far, `TWeakObjectPtr` is usually dereferenced before using it, especially in a loop, to avoid multiple dereferencing, which could be costly. However, I performed a small benchmark and it turned out that dereferencing `TObjectPtr` is more costly than dereferencing `TWeakObjectPtr`. I can also say that I have never seen `TObjectPtr` dereferenced before using it

first loop : TWeakObjectPtr : 62s , TObjectPtr : 79s.  
second loop : 41s.

```auto
{
	TWeakObjectPtr<AActor> WeakActor = GetOwner(); /*TObjectPtr<AActor> WeakActor = GetOwner();*/
	const int64 NumIterations = 10000000000;

	{
		const double Start = FPlatformTime::Seconds();
		for(int64 i = 0; i < NumIterations; ++i)
		{
			WeakActor->SetActorTickEnabled(false);
		}
		const double Duration = FPlatformTime::Seconds() - Start;
		UE_LOG(LogTemp, Warning, TEXT("Dereference every time obj: %f s"), Duration);
	}

	{
		const double Start = FPlatformTime::Seconds();
		AActor* RawActor = WeakActor.Get();
		for(int64 i = 0; i < NumIterations; ++i)
		{
			RawActor->SetActorTickEnabled(false);
		}
		const double Duration = FPlatformTime::Seconds() - Start;
		UE_LOG(LogTemp, Warning, TEXT("One Get() then use raw: %f s"), Duration);
	}
```
