Cache `vals[0]` since you use it many times below -- `var vals0 = vals[0];`