Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 138985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:21:15+00:00 2026-05-11T07:21:15+00:00

As a side result of testing some code I wrote a small function to

  • 0

As a side result of testing some code I wrote a small function to compare the speed of using the array.push(value) method vs direct addressing array[n] = value. To my surprise the push method often showed to be faster especially in Firefox and sometimes in Chrome. Just out of curiosity: anyone has an explanation for it?

Here’s the test (note: rewritten 2023/02/10)

const arrLen = 10_000; const x = [...Array(10)].map( (_, i) => testArr(arrLen, i)); console.log(`Array length: ${arrLen}\n--------\n${x.join(`\n`)}`);  function testArr(n, action) {   let arr = [];   const perfStart = performance.now();   const methods =      ` for (; n; n--) arr.push(n)       for (; i < n; i += 1) { arr[i] = i; }       for (; i < n; i += 1) arr.push(i)       while (--n) arr.push(n)       while (i++ < n) arr.push(n)       while (--n) arr.splice(0, 0, n)       while (--n) arr.unshift(n)       while (++i < n) arr.unshift(i)       while (--n) arr.splice(n - 1, 0, n)       while (n--) arr[n] = n`.split(`\n`).map(v => v.trim());   const report =  i => `${methods[i]}: ${     (performance.now() - perfStart).toFixed(2)} milliseconds`;   let i = 0;      switch (action) {     case 0: for (; n; n--) arr.push(n)     case 1: for (; i < n; i += 1) { arr[i] = i; } break;     case 2: for (let i = 0; i < n; i += 1) arr.push(i); break;     case 3: while (--n) arr.push(n); break;     case 4: while (i++ < n) arr.push(n); break;     case 5: while (--n) arr.splice(0, 0, n); break;     case 6: while (--n) arr.unshift(n)     case 7: while (++i < n) arr.unshift(i); break;     case 8: while (--n) arr.splice(n - 1, 0, n); break;     default: while (n--) arr[n] = n;   }      return report(action); }
.as-console-wrapper {     max-height: 100% !important; }

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:21:15+00:00Added an answer on May 11, 2026 at 7:21 am

    All sorts of factors come into play, most JS implementations use a flat array that converts to sparse storage if it becomes necessary later on.

    Basically the decision to become sparse is a heuristic based on what elements are being set, and how much space would be wasted in order to remain flat.

    In your case you are setting the last element first, which means the JS engine will see an array that needs to have a length of n but only a single element. If n is large enough this will immediately make the array a sparse array — in most engines this means that all subsequent insertions will take the slow sparse array case.

    You should add an additional test in which you fill the array from index 0 to index n-1 — it should be much, much faster.

    In response to @Christoph and out of a desire to procrastinate, here’s a description of how arrays are (generally) implemented in JS — specifics vary from JS engine to JS engine but the general principle is the same.

    All JS Objects (so not strings, numbers, true, false, undefined, or null) inherit from a base object type — the exact implementation varies, it could be C++ inheritance, or manually in C (there are benefits to doing it in either way) — the base Object type defines the default property access methods, eg.

    interface Object {     put(propertyName, value)     get(propertyName) private:     map properties; // a map (tree, hash table, whatever) from propertyName to value } 

    This Object type handles all the standard property access logic, the prototype chain, etc. Then the Array implementation becomes

    interface Array : Object {     override put(propertyName, value)     override get(propertyName) private:     map sparseStorage; // a map between integer indices and values     value[] flatStorage; // basically a native array of values with a 1:1                          // correspondance between JS index and storage index     value length; // The `length` of the js array } 

    Now when you create an Array in JS the engine creates something akin to the above data structure. When you insert an object into the Array instance the Array’s put method checks to see if the property name is an integer (or can be converted into an integer, e.g. ‘121’, ‘2341’, etc.) between 0 and 2^32-1 (or possibly 2^31-1, i forget exactly). If it is not, then the put method is forwarded to the base Object implementation, and the standard [[Put]] logic is done. Otherwise the value is placed into the Array’s own storage, if the data is sufficiently compact then the engine will use the flat array storage, in which case insertion (and retrieval) is just a standard array indexing operation, otherwise the engine will convert the array to sparse storage, and put/get use a map to get from propertyName to value location.

    I’m honestly not sure if any JS engine currently converts from sparse to flat storage after that conversion occurs.

    Anyhoo, that’s a fairly high level overview of what happens and leaves out a number of the more icky details, but that’s the general implementation pattern. The specifics of how the additional storage, and how put/get are dispatched differs from engine to engine — but this is the clearest i can really describe the design/implementation.

    A minor addition point, while the ES spec refers to propertyName as a string JS engines tend to specialise on integer lookups as well, so someObject[someInteger] will not convert the integer to a string if you’re looking at an object that has integer properties eg. Array, String, and DOM types (NodeLists, etc).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer CuteEditor or TinyMCE are probably your best bets. Disclaimer: WYSIWYG… May 12, 2026 at 1:09 am
  • Editorial Team
    Editorial Team added an answer One way would be to fetch the JAD file using… May 12, 2026 at 1:09 am
  • Editorial Team
    Editorial Team added an answer I'd recommend a bit different solution: Use Help Server to… May 12, 2026 at 1:09 am

Related Questions

Given a function, I'm trying to find out the names of the nested functions
My organization has been experimenting with the introduction of more Agile methods. We've been
The Scripting.FileSystemObject TextStream object supports Windows style Unicode and 'plain text' modified by the
I have a fairly simple CRUD web application (C#/ASP.NET) that runs fine in Firefox,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.