Rigidbody - freeze rotation and position?

Whenever an object has a rigidbody I notice in the Unity editor that you can choose to freeze the rotation (x, y, z) and position (x, y, z) under the Rigidbody Constraints. I’m working on a side-scroller and before discovering this I’ve been using a short script that “locks” objects onto the z-axis:
function Update ()
{
transform.position.z = 0;
transform.rotation.x = 0;
transform.rotation.y = 0;
}

Does anyone know which method is more efficient for hardware? Do they both have the same overhead?

Rigidbody Constraints only limit physics effects: if you freeze rotations in all axes, for instance, the rigidbody will not rotate when colliding with an obstacle - but you still can rotate it with Transform.Rotate. Freezing the z axis will disallow the rigidbody to be moved by forces or react to collisions in the z axis, but Translate can move it anywhere without any restriction. If this solve your problem, it’s cheaper to use constraints - physics must do its job anyway, and constraints probably will reduce a little the time spent managing this rigidbody. But if you don’t need a rigidbody, adding it just to have the constraints will be more expensive then using your locking script. By the way, don’t set transform.rotation components directly: this property is a quaternion, and its components have nothing to do with the familiar angles we know. These angles can be read and set in the transform.eulerAngles instead:

function Update(){
    transform.position.z = 0;
    transform.eulerAngles.x = 0;
    transform.eulerAngles.y = 0;
}

when you put freeze on in constraints , object freeze when touch other object.

is it possible in code, saying when i put a key unfreeze ?