DEV Community

Dragoljub Bogićević
Dragoljub Bogićević

Posted on

5 2

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!

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →