paulmillr / es6-shim

ECMAScript 6 compatibility shims for legacy JS engines
http://paulmillr.com
MIT License
3.11k stars 387 forks source link

String#trim consistency with es5-shim #343

Open lewisje opened 9 years ago

lewisje commented 9 years ago

I noticed that this shim uses a single regex, while es5-shim uses two, and although I know the es5-shim project didn't want to get into "performance golfing" toward further optimizing its trim shim, it does turn out to be reliably faster to use two regexes than one: https://jsperf.com/mega-trim-test/47

(Comparing character codes against numbers is even faster than the native String#trim, but it results in code that is more difficult to understand, particularly in this jsPerf, which uses decimal literals.)

Also, why does this implementation use capturing groups?

ljharb commented 9 years ago

At the time the es6-shim implementation was written, the es5-shim implementation also used 1 regex. When that was updated for performance to 2 regexes, this one wasn't.

A PR that updates this one to not use capturing groups, and/or to use 2 instead of 1 regex, would be great.