JavaScript - Array Execution Speed

bogicevic7

Dragoljub Bogićević

Posted on March 13, 2020

JavaScript - Array Execution Speed

Did you ever wonder about execution speed of JavaScript arrays? Neither do I. So let's find out what is going on when using different approaches for array creation.

To create an array in JavaScript we can use array literal like this:

let testArray = [1, 2, 3];

or we can use the keyword new like this:

let testArray = new Array(1, 2, 3);

The two examples above do exactly the same, create new array with values. But what is the difference, which approach we should use and why?

To find out let's test the first snippet here.

After each code execution I refreshed the page
and results varied between ~93ms and ~99ms.

So, let's do the same with second code snippet here.

For some reason, results variations are more common, so I had results from ~1500ms to even ~7500ms.

So, the outcome of this simple post is kinda obvious:

For simplicity, readability and execution speed, use the first one (the array literal method).

Thanks for reading!

💖 💪 🙅 🚩
bogicevic7
Dragoljub Bogićević

Posted on March 13, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related