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 8052785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:38:43+00:00 2026-06-05T07:38:43+00:00

Tell me please, what is the faster way to push a value into array?

  • 0

Tell me please, what is the faster way to push a value into array?

I have written a simple test to compare push and a[a.length]= methods:

push.js:

self.onmessage = function (event) {
  var n = Number(event.data),
      i,
      pushAr = [],
      pushStart,
      pushDuration,
      ar = [],
      start,
      duration,
      message;

  // Push
  pushStart = new Date();
  for (i = 0; i < n; i += 1) {
    pushAr.push(i);
  }
  pushDuration = new Date() - pushStart;

  // explicit
  start = new Date();
  for (i = 0; i < n; i += 1) {
    ar[ar.length] = i;
  }
  duration = new Date() - start;

  message = "N = " + n;
  message += "\nPush duration: " + pushDuration + "; ";
  message += "Length duration: " + duration;

  self.postMessage(message);
};

main.js:

var worker = new Worker("push.js"),
    i;

worker.onmessage = function (event) {
  console.log(event.data);
};

for (i = 4; i < 8; i += 1) {
  worker.postMessage(Math.pow(10, i));
}

And I got following results:

Chrome:

N = 10000
Push duration: 0; Length duration: 0
N = 100000
Push duration: 3; Length duration: 5
N = 1000000
Push duration: 56; Length duration: 90
N = 10000000
Push duration: 807; Length duration: 948

Safari:

N = 10000
Push duration: 1; Length duration: 4
N = 100000
Push duration: 2; Length duration: 2
N = 1000000
Push duration: 27; Length duration: 41
N = 10000000
Push duration: 283; Length duration: 461

FireFox:

N = 10000
Push duration: 1; Length duration: 0
N = 100000
Push duration: 2; Length duration: 2
N = 1000000
Push duration: 11; Length duration: 20
N = 10000000
Push duration: 279; Length duration: 412

Seems like Array.push is faster, but why some libs use array length instead? And what bonuses of such method? Why Chrome is so slow? Maybe some faster methods exist?

  • 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. Editorial Team
    Editorial Team
    2026-06-05T07:38:45+00:00Added an answer on June 5, 2026 at 7:38 am

    Both methods are exactly the same, push is faster because the same algorithm (getting array length) works inside the script engine and not in the script. ar[ar.length] may be used if you need the result of that expression to be the new value, like in chained assigment somevar = ar[ar.length] = "some new value"

    I found some other possible use – compatibility with older versions of javascript engine. In Microsoft documentation for JScript version 5.0 there’s no mention of push method in Array object. And I actually found my own code from back then (around 2000) that actually uses images[images.length] = s code. So, maybe arrays didn’t always have the push method.

    P.S. Yes, MSDN cocumentation for push method says version 5.5 is required and that wasn’t even in windows 2000.

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

Sidebar

Related Questions

Please tell me why i am getting ClassCastException in this case I have type
Tell me please, how to use getBulkUserInformationFor: method correctly? I have Could not authenticate
Please tell me which out of the two methods, executeUpdate and execute is the
could you please tell me which one of these two sentences is faster? 1st
Tell me please, what is the sacred power of the style below: var javascript
Tell me please, how can I create xsd schema, which successfully validate the following
Please tell me how I can hide textarea in td table, what attribute must
Please tell me what is the Qt equivalent function for glutswapbuffers()..
Please tell me why my window doesn't render. Below is the javascript that i
Please tell me if it is possible to do the following: create an instance

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.