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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:33:11+00:00 2026-06-15T17:33:11+00:00

I have a two dimensional associative array which contains a map of blocks that

  • 0

I have a two dimensional associative array which contains a map of blocks that need to be drawn in a html5 canvas.

It currently takes around 28ms to complete it, which is too much (I’m running the code 60 times a second), I need to run though the matrix more efficienty somehow. This is my code:

for(var x=0; x<Object.keys(matrix).length; x++){ // Run through cols
    col = Object.keys(matrix)[x];
    for(var y=0; y<matrix[col].length; y++){ // Run though rows
        row = Object.keys(matrix[col])[y];
        if(matrix[col][row] != '0'){
            drawRect(col,row,blockSize,blockSize);
            }
        }
    }

I hope it’s inefficient and something can be corrected. How can you loop the total of a 2D array most efficiently?

  • 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-15T17:33:11+00:00Added an answer on June 15, 2026 at 5:33 pm

    You must use the double for loop to loop an entire 2-D array. As with trying to speed up any code with a performance issue, the very first step is to figure out what is taking the most time and work on your optimizations in that element of the issue first.

    The likely guess is that drawRect takes way more time than any of the issues in the loop, but if you want to speed up the rest of the code, you can do these things:

    1. cache Object.keys(matrix) so it is not constantly recalculated
    2. cache the stop value for each for loop so it is not re-computed every time through each loop
    3. cache the column keys so it is not recalculated every time through the inner loop
    4. cache the matrix column

    That code would look like this:

    var keys = Object.keys(matrix);
    for(var x = 0, lenX = keys.length; x < lenX; x++) { // Run through cols
        col = keys[x];
        var colKeys = Object.keys(matrix[col]);
        var matrixCol = matrix[col];
        for(var y = 0, lenY = matrix[col].length; y < lenY; y++) { // Run though rows
            row = colKeys[y];
            matrixCol[row] != '0'){
                drawRect(col,row,blockSize,blockSize);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a two dimensional array that I need to sort numerically. Here is
I have a two dimensional array in php that I need to express as
I have a two dimensional array that contains pairs of strings. If one string
I have a two dimensional array that I need to load data into. I
I have a two dimensional array of points, which constitute a map. The black
I have a two dimensional JSON array where each element contains several attributes. The
I have a two dimensional ArrayList that I need to filter the duplicates out
I have the following three arrays and need to create a new two-dimensional array
I have a two-dimensional character array board[row][col] , which is designed to simulate a
Assume, I have a two dimensional array A , and it's stated that somewhere

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.