Performance optimisation

Based on the efforts by @epmatsw. The native trim() function is now used with a fallback for non-supporting browsers.