3 ms·
For me, Copilot has had the opposite effect from skill rot: it's pretty lousy at producing big chunks of code so I tend to use it for small chunks where I have
by lolinder 3y ago
For me, Copilot has had the opposite effect from skill rot: it's pretty lousy at producing big chunks of code so I tend to use it for small chunks where I have a pretty good sense for what I want to do. However, sometimes it surprises me by offering something that I wouldn't have chosen that turns out to be far better, and I learn new things from those suggestions.
For example, I gave Copilot something like this:
const textFile = longfile
const numLines =
I expected it to give me a regular expression that would count the number of new line characters, but instead it split the string on \n and asked for the length of that array. These are decent sized files (300ish lines) and this code is running in a hot loop over thousands of them, but I decided to give it a chance and benchmarked both choices.
It turns out that even with all of the extra allocations, the regular expression solution was 4-5x slower than splitting and getting the length. Not something I would have guessed, but now I know.
- adalacelove 3y agoI think both solutions vs just counting the number of newlines are pretty bad
- morelisp 3y agoYou'll never achieve takeoff if you keep thinking like that.
- lolinder 3y agoHaha, yep, an iterator would definitely be faster than either! I'm coming from Kotlin where I expect there to be a standard library function for simple tasks like this, but I clearly need to get back in the habit of writing loops by hand.
- electroly 3y agoIf you care about performance, those are both insane ways to count newlines. Try a loop of indexOf() and watch it absolutely spank the splitting method. That was the first thing that popped into my head, and in testing it appears to be 5x the speed of splitting (and 0x the memory consumption).
- lolinder 3y agoHaha, clearly this was a bad example, because you're right, both are bad. I'm recently coming back to JavaScript after years of Kotlin, and I've clearly gotten too comfortable with standard libraries that have decent one-liner functions for this kind of thing. I'm patching in a loop now (though I feel like a regular for loop is more readable than a loop with indexOf).
- electroly 3y agoindexOf() is faster than the naive character-by-character loop because (presumably) it does all the work outside of JavaScript. You only run JavaScript code for each newline that is found rather than for every character. indexOf() may even use SIMD instructions for speed.
- lolinder 3y agoAh, good point. In my case, after benchmarking inside my actual app on real data, all three perform equally well (including the String.split version). Dropping the Regex made the difference between this being the heavy part of the process to it being a rounding error, and there isn't much additional value to be gained. With that in mind, I'd rather go for legibility over speed. I'm going to go with the for loop, though, because the extra memory usage could come back to bite me later.