5 ms·
We have. I spent some time optimizing [0] a String.repeat function over at Stackoverflow and I was surprised that many developers today don't know what they are
by nikolay 11y ago
We have. I spent some time optimizing [0] a String.repeat function over at Stackoverflow and I was surprised that many developers today don't know what they are doing, including core team members [1]. Specifically,
function repeatString(str, len) {
return Array.apply(null, {
length: len + 1
}).join(str).slice(0, len)
}
[0]: http://stackoverflow.com/questions/202605/repeat-string-javascript/36173160#36173160 http://stackoverflow.com/questions/202605/repeat-string-java...
[1]: http://stackoverflow.com/questions/202605/repeat-string-javascript/35319469#35319469 http://stackoverflow.com/questions/202605/repeat-string-java...
- Macha 11y agoSo I don't think this is an example of core team members who don't know what you're doing. This function repeats a string up to a certain length. e.g. repeatString(foo, 10) = foofoofoof This could entirely be what the developers needed in their code base. The fact that it doesn't repeat a string N times, is at best, an example of bad naming. The issue here is that someone unrelated to the core team grepped the node codebase for the term they wanted and threw a function up as a SO answer without understanding what it did.
- nikolay 11y agoAllocate and join an array of blanks just to repeat a string is acceptable?!
- Macha 11y agoI guess I'm also as guilty of looking at input/output rather than implementation here.
- nikolay 11y agoHere's a benchmark [0]! [0]: https://jsfiddle.net/nikolay/Lgshxrk8/ https://jsfiddle.net/nikolay/Lgshxrk8/
- jestar_jokin 11y agoJoining an array of strings is sometimes better than concatenating a string in place, yes. Consider the following: 1: var str = 'foo'; 2: str += 'bar'; 3: str += 'baz'; At program load, you have three string constants, 'foo', 'bar', and 'baz'. At 1, 'str' becomes a pointer to the string constant 'foo'. At 2, 'str' becomes a pointer to a string 'foobar'. Memory is allocated to store this string. At 3, 'str' becomes a pointer to a stinrg 'foobarbaz'. Memory is allocated to store this string. Now consider the following: 1: var str = ['foo', 'bar', 'baz'].join(''); At program load, you have three string constants, 'foo', 'bar', and 'baz'. At 1, 'str' becomes a pointer to the string constant 'foobarbaz'. In the array concatenation, the JS interpreter can create a single string buffer large enough to contain the contents of each array element and the separator string * N - 1 (where N is the number of elements), and return a pointer. Once you have a large number of elements, or are joining large strings, the performance difference (especially regarding garbage collection) could matter.
- nikolay 11y agoYou'll be surprised, but my implementation is, roughly speaking, 10 (Chrome) to 100 (Safari) to 200 (Firefox) times faster [0], depending on the browser! [0]: https://jsfiddle.net/nikolay/Lgshxrk8/ https://jsfiddle.net/nikolay/Lgshxrk8/
- deleted 11y ago[deleted]