Web Development

JS MythBusters - An optimization handbook from a high level point of view

Written by Kevin Liew on 04 Jun 2017
4,211 Views • Shares

Get some more tips and trick for performance optimised Javascript. Here we have a collection of good practices to help you gain back some performance.

One of the examples is the preallocation of Array. Here's a few paragraph from JS Mythbusters:

Instead of allocate a new Array every time, a good approach in terms of perfomance is reuse the same array instance.

For clean the elements of the Array, you can follow different approach.

Probably the most evident is use .pop in a loop for clean all the elements:

const array = [1, 2, 3, 4, 5]
while (array.length) array.pop()
console.log(array) // []

However, there is a better and simpler using Array.prototype.length. This property is writable, and, if you set the value to 0, remove all the elements:

const array = [1, 2, 3, 4, 5]
array.length = 0
console.log(array) // []

Mind-blown yet? Check out the rest from JS Mythbusters

Join the discussion

Comments will be moderated and rel="nofollow" will be added to all links. You can wrap your coding with [code][/code] to make use of built-in syntax highlighter.