es6-shim icon indicating copy to clipboard operation
es6-shim copied to clipboard

String#trim consistency with es5-shim

Open lewisje opened this issue 10 years ago • 1 comments

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?

lewisje avatar May 11 '15 08:05 lewisje

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.

ljharb avatar May 11 '15 08:05 ljharb