Array 1…N in JavaScript: Secrets of Speed and Performance

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Array 1…N in JavaScript: From Simple to Performant

\n\n

Hello, fellow developer! If you've ever faced the task of "creating an array of numbers from 1 to N" — you're not alone. This is such a popular question on Stack Overflow that it has gathered thousands of views and dozens of answer options. It seems like it couldn't be simpler, right? But the devil, as always, is in the details: performance, readability, and code versatility.

\n\n

Today we'll break down all the main ways to create an array [1, 2, 3, ..., N] in JavaScript, comparing them by speed, elegance, and use cases. You'll learn which method to choose for a regular project, and which one when every millisecond counts. Let's go!

\n\n

Why Is This Important? Task Context

\n\n

Imagine: you're writing a sorting algorithm, generating test data, creating a grid for a game, or just want to quickly get a sequence of numbers. In all these cases, you need an array from 1 to N. It seems simple — a for loop and go. But modern JavaScript offers so much syntactic sugar that it's easy to get confused.

\n\n

We'll look at 5 main approaches, from classics to modern one-liners, and test them under real-world conditions. Get your browser console and favorite editor ready — it's going to be intense!

\n\n

Method 1: Classic for Loop

\n\n

A basic but reliable method that every beginner knows:

\n\n
function createArrayLoop(n) {\n  const arr = [];\n  for (let i = 1; i <= n; i++) {\n    arr.push(i);\n  }\n  return arr;\n}\n\nconsole.log(createArrayLoop(5)); // [1, 2, 3, 4, 5]
\n\n

Pros:

\n
    \n
  • Maximum readability — understandable even for those just starting to learn JS.
  • \n
  • No hidden calls or magic.
  • \n
  • Works in all environments, including very old browsers.
  • \n
\n

Cons:

\n
    \n
  • Slower than some alternatives (but not critical for small N).
  • \n
  • Lots of code for a simple task.
  • \n
\n\n

Method 2: Array.from() with map Function

\n\n

An elegant modern approach introduced in ES6:

\n\n
const createArrayFrom = n => Array.from({ length: n }, (_, i) => i + 1);\n\nconsole.log(createArrayFrom(5)); // [1, 2, 3, 4, 5]
\n\n

Here we create an array with a specified length and immediately fill it using a map function. The second argument of Array.from is the mapper function, which receives the current index i and returns i + 1.

\n\n

Pros:

\n
    \n
  • Very concise and expressive.
  • \n
  • Doesn't require creating an empty array beforehand.
  • \n
  • Good performance.
  • \n
\n

Cons:

\n
    \n
  • May be less obvious for beginners.
  • \n
  • Creates an intermediate object { length: n }.
  • \n
\n\n

Method 3: Spread Operator + keys()

\n\n

One of the most popular one-liners on Stack Overflow:

\n\n
const createArraySpread = n => [...Array(n).keys()].map(i => i + 1);\n\nconsole.log(createArraySpread(5)); // [1, 2, 3, 4, 5]
\n\n

How does it work? Array(n) creates a sparse array of n empty elements. .keys() returns an iterator of indices (0, 1, 2...). The spread operator expands it into a regular array, and then map shifts the values by +1.

\n\n

Pros:

\n
    \n
  • Very concise (one call).
  • \n
  • Looks "magical" — pleasantly surprises colleagues during code review.
  • \n
\n

Cons:

\n
    \n
  • The slowest of all methods for large N (due to double iteration and creating intermediate arrays).
  • \n
  • Harder for beginners to understand.
  • \n
  • Consumes more memory.
  • \n
\n\n

Method 4: Array.fill() + map()

\n\n

Another popular option:

\n\n
const createArrayFill = n => Array(n).fill().ma                

Blogs

Book Recommendations