News for hackers. Stuff that matters.
Re:My Node.js is a bit Rusty by disgruntledphd2 on Friday September 08, 2023 @04:01AM (#37430841)
It's probably all the memory allocations. I know that when I do work like this, creating a huge array up front and assigning to that typically results in massive speedups.

I know basically nothing about Node though, so I could be completely wrong here.

Re:My Node.js is a bit Rusty by Karrot_Kream on Friday September 08, 2023 @04:25AM (#37431009)
Yeah that's one way to do it. Another is to reuse structures where possible to not force reallocation. There's a bunch of ways.
Re:My Node.js is a bit Rusty by xmcqdpt2 on Friday September 08, 2023 @07:07AM (#37432054)
Node.js has a decent JIT, or so I've heard. I have experience with the HotSpot JVM. For the JVM this code would absolutely stop allocating on the heap past 10k iterations of the loop. "Split a string then grab the parts in a hot loop" is ground stakes for JIT compilers.

If they are seeing any GC activity once the loop gets hot, something else is going very wrong.

Re:My Node.js is a bit Rusty by disgruntledphd2 on Friday September 08, 2023 @11:26AM (#37434819)
Yeah but if you're appending in a loop you'll need to allocate memory every time, right? Can a JIT avoid or rewrite this?
Re:My Node.js is a bit Rusty by xmcqdpt2 on Friday September 08, 2023 @07:33PM (#37440580)
Yep the Hotspot JIT will absolutely allocate on the stack and reuse memory allocations inside loops. It also has escape analysis and it will detect you don't need the array of strings after the iteration. In that case it usually just allocates the strings directly at their final locations on the record object.
Re:My Node.js is a bit Rusty by disgruntledphd2 on Saturday September 09, 2023 @11:12AM (#37446305)
Wow, that's super cool. Almost makes me wish i did stuff on the JVM (but until someone rewrites LAPACK for it, that seems unlikely).