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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:12:04+00:00 2026-06-15T15:12:04+00:00

I have a game and initializing a map 2000 x 4000 blocks. It runs

  • 0

I have a game and initializing a map 2000 x 4000 blocks.
It runs only once onLoad and takes ~700ms. How can I speed it up? Other logic depends on this map.
Here is the code:

var start = new Date();
var g = {};
g.world = { h:2000, w:4000, cellInfo: [] };
var i, j,
    world = g.world,
    hlim = world.h,
    wlim = world.w,
    cellInfo = world.cellInfo;
for ( i = hlim; i; i--) {
    cellInfo[i] = [];
    for (j = wlim; j; j--) {
        cellInfo[i][j] = 1;
    }
}
g.world.cellInfo = cellInfo;
alert(new Date() - start);​

Here is fiddle: http://jsfiddle.net/NSX9z/

  • 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-15T15:12:06+00:00Added an answer on June 15, 2026 at 3:12 pm

    A couple of options for you:

    Lazy init

    Your best bet for speeding it up is to use lazy initialization instead. E.g., instead of initializing all 8 million slots, have the code the relies on them handle it if they’re not there. E.g., to get a cell

    function getCell(x, y) {
        var row, cell;
    
        row = cellInfo[x];
        if (!row) {
            row = cellInfo[x] = [];
        }
    
        cell = row[y];
        if (typeof cell === "undefined") {
            cell = row[y] = 1; // 1 appears to be the default value
        }
    
        return cell;
    }
    

    …and similar for setCell. That spreads out the init. Of course, it means things are slightly slower, though it’s unlikely to be a perceptible difference.

    Array cloning

    If you don’t do that, another option is to create the 4,000-slot array once, then clone it rather than creating it by hand, in hopes that doing the work inside the JavaScript engine is faster than doing the actual loop yourself:

    // ...
    var the4k = [];
    // ...build it...
    // Now use it for one row and clone it for the others
    cellInfo[hlim] = the4k;
    for ( i = hlim - 1; i; i--) {
        cellInfo[i] = the4k.slice(0);
    }
    

    Array cloning speeds things up dramatically for me, Chrome goes from ~780ms to ~130ms, IE9 from ~530ms to ~50ms.

    The other issue to consider is that on IE8 and earlier, the “slow script warning” isn’t based on time, but on number of operations. Your fiddle gives me the slow script warning on IE8. Mine doesn’t.

    IE8 argues for lazy-init, though, as it takes nearly 9 seconds to run even my version of the fiddle.

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

Sidebar

Related Questions

I have a game which displays an array of colored blocks. The user can
I have a game that currently runs under Windows and Mac OS X and
I have a game showing ads through AdWhirl. Ads are only shown when the
I'm making flash game, and I have the staged masked so your character can
I'm developing a game. I want to have game entities each have their own
Update The reason I couldn't update properly was I didn't have game's jar file
I am trying to code a global lookup table of sorts. I have game
I have a game where user controls a character (with his finger) and i
I have this game where balloons are coming from bottom and player has to
I have a game engine that uses OpenGL for display. I coded a small

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.