DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. JavaScript
  4. JavaScript: Sparse Arrays vs. Dense Arrays

JavaScript: Sparse Arrays vs. Dense Arrays

Axel Rauschmayer user avatar by
Axel Rauschmayer
·
Jun. 21, 12 · Interview
Like (0)
Save
Tweet
Share
6.06K Views

Join the DZone community and get the full member experience.

Join For Free
In general, arrays in JavaScript are sparse – they can have holes in them, because an array is simply a map from indices to values. This blog post explains how to create dense arrays, arrays without holes.

Sparse arrays

Creating a sparse array of a given length is simple:
    > var a = new Array(3);
    > a
    [ , ,  ]
    > a.length
    3
    > a[0]
    undefined
When you iterate over it, you can see that it has no elements. JavaScript skips the holes.
    > a.forEach(function (x, i) { console.log(i+". "+x) });

    > a.map(function (x, i) { return i })
    [ , ,  ]

Dense arrays

Brandon Benvie recently mentioned a trick for creating a dense array on the es-discuss mailing list:
    > var a = Array.apply(null, Array(3));
    > a
    [ undefined, undefined, undefined ]
The above invocation is equivalent to
    Array(undefined, undefined, undefined)
For many things, there is not much of a difference between this array and the previous sparse array:
    > a.length
    3
    > a[0]
    undefined
However, you can now iterate over the elements, e.g. to fill the array with values:
    > a.forEach(function (x, i) { console.log(i+". "+x) });
    0. undefined
    1. undefined
    2. undefined

    > a.map(function (x, i) { return i })
    [ 0, 1, 2 ]

One more trick

The email also mentions the following trick:
    > Array.apply(null, Array(3)).map(Function.prototype.call.bind(Number))
    [ 0, 1, 2 ]
This is roughly the same as
    Array.apply(null, Array(3)).map(
        function (x,i,...) { return Number.call(x,i,...) })
Note that x is the first parameter of call and specifies the value of this. Number being a function, that value is ignored. I prefer the more explicit variant shown above:
    Array.apply(null, Array(3)).map(function (x,i) { return i })

Useful in practice?

In practice, creating a dense array in the manner described above will make your code difficult to understand for others. It is thus better to use utility functions such as _.range:
    > _.range(3)
    [ 0, 1, 2 ]
Combine it with map, in order to fill an array with a given value.
    > _.range(3).map(function () { return "a" })
    [ 'a', 'a', 'a' ]

Related posts

  1. Iterating over arrays and objects in JavaScript
  2. Trying out Underscore on Node.js
JavaScript

Published at DZone with permission of Axel Rauschmayer, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DevOps for Developers — Introduction and Version Control
  • What’s New in the Latest Version of Angular V15?
  • All the Cloud’s a Stage and All the WebAssembly Modules Merely Actors
  • Developer Productivity: The Secret Sauce to Building Great Dev Teams

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: