3 ms·
How to achieve 218.1% better performance when replacing strings in JavaScript
- _bxg1 7y agoSplit and join is worse because it allocates N junk strings, all of which have to be fully copied only to be copied again and then thrown away. This is the same problem as the "Array.push() is 945x faster than Array.concat()" post from last week: https://news.ycombinator.com/item?id=19991339 https://news.ycombinator.com/item?id=19991339 As I commented on the other post, all programmers really should spend some time learning how memory, pointers, data structures, heap vs stack, etc. work. Even if you aren't writing C++ at your day job, that stuff matters.
- Bino 7y agoWhy would anyone use split/join?
- piyiotisk 7y agoMaybe that’s what some people use. For example deepcloning objects in JS is faster with json.parse(json.stringify())