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

  • Home
  • SEARCH
  • 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 8544755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:38:12+00:00 2026-06-11T12:38:12+00:00

I was considering ways to create arrays containing a default value using native methods

  • 0

I was considering ways to create arrays containing a default value using native methods and ended up with

function pushMap(length, fill){
    var a = [], b = [];
    a.length = length;
    b.push.apply(b,a);
    return b.map(function(){return fill;});
}

Expecting it to be 2 or 3 times slower than a while loop, as the native methods have to loop twice whereas while loops only once, so I compared it on jsperf against

function whileLengthNew(len, val) {
    var rv = new Array(len);
    while (--len >= 0) {
        rv[len] = val;
    }
    return rv;
}

and it is actually 18 to 27 times slower (tested with Google Chrome on Ubuntu, browsers/OSs welcome).

What is happening that causes such a big difference?

  • 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-11T12:38:13+00:00Added an answer on June 11, 2026 at 12:38 pm

    I would expect that this is due to two major factors:

    1. Memory allocation — whileLengthNew creates an array of the correct size first, and then operates on it, pushMap creates the final array one element at a time with map. This may cause multiple allocations, especially if the source array is large. (The way that you create the initial a and b arrays is basically irrelevant, since map is building up a new array to return anyway — it doesn’t actually change anything in b)

    2. Function call overhead — in your call to map, you are calling a function for every element of the array. This involves quite a lot of overhead; setting up activation records and scope chains, stack manipulation, and passing the return value back. — all of this to access a variable which is constant within the function. On top of that, you have set up a closure, so even accessing the fill variable is slower than it is in the whileLengthNew version.

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

Sidebar

Related Questions

I'm considering using this: http://phpgoogle.blogspot.com/2007/08/four-ways-to-generate-unique-id-by-php.html My idea is to use a mixture between 2
Considering this code with 3 differents function call semantics: void f(void){ puts(OK); } int
I have almost completed my Android application so am considering ways of distributing the
I have programmed an embedded software (using C of course) and now I'm considering
I am considering best option to create multi-purpose admin page in my asp.net application.
I am about to create a jQuery plugin but I was thinking of using
I'm considering ways to develop a site, planning to get some 100-10.000 visitors daily.
I'm considering using Powershell scripts for triggers on our Perforce server. Unfortunately, even on
I've recently (re-)discovered SlickGrid, and am considering using it for a project I'm working
I've been considering using Google App Engine for a few hobby projects. While they

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.