Is javascript the best practice or most efficient way to perform this function?