In my RTS I loop through each player, and their array of units (in Lite-C) and check if it is within sightrange. It gets more complicated, I check if it is on a different team with no diplomatic relations. Then I store the entity pointer if it's health is lower than the previous one, which in the end has the enemy unit in sightrange with the lowest heath for the unit to attack (if in aggresive mode, otherwise the function is aborted).
Try something like that in C-Sctipt. It is fast because no c_trace or c_scan is needed, all done with maths in one frame.