Hacker News new | past | comments | ask | show | jobs | submit login
How to achieve 218.1% better performance when replacing strings in JavaScript (picocoder.io)
2 points by piyiotisk on May 30, 2019 | hide | past | favorite | 3 comments



Split 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

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.


Why would anyone use split/join?


Maybe that’s what some people use. For example deepcloning objects in JS is faster with json.parse(json.stringify())




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: