Rather than calculating this each time, perhaps you can calculate the length only when you notice that the input value has changed?