3 ms·
Well, you'd be wrong by a mile! On my machine, the .map(..) way is almost twice as slow. It ain't even close. But you're right, I failed to consider that it'
by arh68 9y ago
Well, you'd be wrong by a mile! On my machine, the .map(..) way is almost twice as slow. It ain't even close.
But you're right, I failed to consider that it's "probably optimized". And "C++". /s
- coldtea 9y agoI don't see how the "sarcasm" (I use the term loosely) is justified. You indeed failed to consider all those things, and all those things are things one should have in mind, whether a particular built-in function is faster than a custom implementation or not. Not to mention that I said "probably optimized C++" -- I didn't claim straight out that this is the case. The other two claims still hold: it's built in, so you don't need to roll your own regex based one (complete with bugs the first time for most people), and it conveys the intention to trim perfectly. That's even better than being faster, unless there's a real need to have that particular operation be fast in your code. Heck, the fact that you got them wrong in your initial regexes (not capturing what trim does in general) shouldn't give you enough pause? If you have gone with those BS regexes instead of trim you would have introduced a bug in the codebase. If one wants to optimize some such method, running on a tight loop, and on a verified hot spot of their code, they would of course need to measure to see which method is faster, not just rely on someone's suggestions. That's computer science 101. That it doesn't work "on your machine" (and on whatever browser you've checked) doesn't mean anything. One would need to check on various browsers, teat what browsers they want to target (e.g. perhaps their app is Electron and only needs to work there), and which method is faster there.