3 ms·
I just looked at the source code of left-pad for the first time, and it's strange. They noticed that concatenating a bunch of single-space strings is O(n^2), an
by cousin_it 7y ago
I just looked at the source code of left-pad for the first time, and it's strange. They noticed that concatenating a bunch of single-space strings is O(n^2), and optimized it to O(n log n) by using repeated doubling. So in the end they take longer than O(n) to fill an array with a constant value. Why?
https://github.com/left-pad/left-pad/blob/master/index.js https://github.com/left-pad/left-pad/blob/master/index.js
- rhardih 7y agoWhy write code to left pad your strings, when you can get an O(n log n) implementation just like that. You only need a "minimal" amount of bundle tooling and a few hundred MB of node_modules. Count your blessings. This is the way.