What is faster Rigidbody.velocity or assigned variable?

I use the velocity and position of an object about 20 times inside a function.
In my case, it probably does not make a difference.
Is using Rigidbody.velocity.x faster than assigning a float to it and using that?
At what point if ever, would it be faster to assign a variable?

So if this were my code:

Rigidbody2D rb = gameObject.GetComponent<Rigidbody2D>();
float vx = rb.velocity.x;

Would it be better to include line 2 and use the variable or just use rb.velocity.x?

At a fundamental level, if you want to compare performance between values types and usage, just try it!

For example:

void VariableAccessSpeedTest()
{
	float assignedValue;
	float readValue;
	float startTime;
	float endTime;
	
	// Assign value 100k times
	// Source: Rigidbody->Vector3->float
	startTime = Time.realTimeSinceStartup;
	for(int i = 0; i < 100000; i++)
	{
		assignedValue = rb.velocity.x;
	}
	endTime = Time.realTimeSinceStartup;
	Debug.Log(string.Format("100k assignments (Rigidbody->Vector3->float): {0} seconds", endTime - startTime));
	
	// Assign value 100k times
	// Source: float
	startTime = Time.realTimeSinceStartup;
	readValue = rb.velocity.x;
	for(int i = 0; i < 100000; i++)
	{
		assignedValue = readValue;
	}
	endTime = Time.realTimeSinceStartup;
	Debug.Log(string.Format("100k assignments (float): {0} seconds", endTime - startTime));
}

Barring the most extreme circumstances (i.e. a HUGE number of uses at a time or actual GetComponent/Find calls or similar), the differences in variable access speed should be so negligible, however, that creating and assigning the new float value could very well wind up being SLOWER than using them in place a few times.

That doesn’t mean you won’t see a potential speed advantage in using “temporary” variables, however. It’s important to note, however, that completely local variables will be much slower than ones that are not. For example:

// Declared outside any region where it's used
// Declared once
float tempVariableFastest;
void Update()
{
	// Declared outside the region where it's used most
	// But still declared every pass through the function
	float tempVariableFast;
	for(int i = 0; i < 100000; i++)
	{
		// Declared immediately before use
		// Redeclared on every pass through the loop
		float tempVariableSlow;
	}
}