# Is it really more efficient to have a local variable for transform here or is Rider being over-zealous?

I'm just going through a quick C# / Unity crash course (coming from Java) and I recieved a warning about "Repeated property access of a built-in component is inefficient" while working in Jetbrains Rider for the following code snippet.

if (Mathf.Abs(horizontalMovement) > Mathf.Epsilon) {
horizontalMovement += horizontalMovement * Time.deltaTime;
var position = transform.position;
horizontalMovement += position.x;

position = new Vector2(horizontalMovement, position.y);
//warning is for this line
transform.position = position;
}


It was already complaining about using transform.position.x and transform.position.y which seemed fair enough so I created the position var but would declaring var cachedTransform = transform; ...; cachedTransform.position = position really be more performant here or is the overhead of creating the local variable just going to negate it?

I'm not asking if this is a valid warning in general, it seems to be. I'm asking if it's valid when you're literally just reassigning in the same statement as I am in the last line of my code above

For reference here's what I would end up with if I followed the warning -

 if (Mathf.Abs(horizontalMovement) > Mathf.Epsilon) {
horizontalMovement += horizontalMovement * Time.deltaTime;
var cachedTransform = transform;
var position = cachedTransform.position;
horizontalMovement += position.x;

position = new Vector2(horizontalMovement, position.y);
cachedTransform.position = position;
}

• Whenever there's question of "slow" code, profile it. I was told something was slow on Pro Feel Golf. I profiled it and found out that on 500,000,000 executions of the "slow" thing, we lost 0.5 seconds. Ordinarily this thing wouldn't be called more than 10-100 times. So yes, it was slower, but it was irrelevant. – Almo Aug 12 at 13:56