node.js left-pad

1 · Nelson Elhage · April 7, 2016, 3:56 a.m.
If you’re a programmer, there’s a good chance you noticed the node.js left-pad fiasco of a few weeks back that temporarily broke most of the npm ecosystem. This blog doesn’t have an opinion on any of that. However, in the ensuing kerfluffle, several people observed that left-pad appears to be quadratic, and that is definitely this blog’s concern. If we look at the code, we see that the main loop looks like so: while (++i < len) { str = ch + str; } In most languages’ string implementations, th...