- Never construct or mutate objects in a loop or recursive call.
- Preallocate the max of what you'll need at load time, then index into it.
- Prefer typed arrays over objects.
- Don't use strings. Work with numbers.
- Remove function calls.
- Do not call a function with differently shaped arguments.
- If the JS engine can't prove a number is a 32 bit int, you'll take a large perf hit.
- Break out your algo into a worker. Not only does it keep things responsive, but it might actually be faster due to less GC pressure.
- The web stack is full of frightening gotchas that you'll discover. Recieving large data on a WebSocket can halt your DOM rendering. Leaking a Promise callback can completely blow a function into interpreter mode. And other fun times.
Finally: If you're hand optimizing and not getting sufficient results on your preferred browser, accept that you're screwed. The next browser will break your assumptions. Rethink your algorithm and data flow, or move it elsewhere, like a worker or server.I do agree the tooling for this stuff isn't great, but I found Chrome's line-by-line perf counts to be excellent hints about where you could do better. It's often surprising but obvious in retrospect.