Common performance pitfalls in JavaScript

Common performance pitfalls in JavaScript

Most people aren't thinking about speed when they are coding in JavaScript. It's most common use case is as the glue between HTML and more complicated user interaction. It's a very pragmatic programming language in that sense.

As JavaScript continues to maneuver its way into every programming niche it can, performance will become increasingly more important. The V8 JavaScript engine was a godsend for JavaScript's ubiquity almost solely because of how performant it made it. Performance really starts to matter when you are writing a server that is handling thousands of requests a second or trying to write responsive UI.

JavaScript is also becoming a much more expressive language. There are an ever increasing amounts of ways you can solve the same problem with different methods. Some of these are more performant than others.

This example demonstrates, four different ways you can reverse an array. The time it takes to run all of these is unnoticeable for the average use case. When you start to ramp up number of operations, it really becomes apparent which are the faster methods.

Object creation is an expensive operation in JavaScript. It doesn't matter whether it is implicit like with the new keyword or through not-mutating methods like splice. If performance matters to your JavaScript application, it is a good rule of thumb to avoid it whenever you can.