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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:12:03+00:00 2026-06-06T05:12:03+00:00

Can anyone suggest a way to shave any time off this script? var countObject

  • 0

Can anyone suggest a way to shave any time off this script?

var countObject = new Object();
var length = data.length;
for(var i = 0; i < length; i += 4) {
  var key = data[i] + ',' + data[i+1] + ',' + data[i+2];
  if(typeof(countObject[key]) == 'number') {
    countObject[key]++
  } else {
    countObject[key] = 0
  }  
}

It is to build up a count of occurrences of RGB values found in data retrieved from a canvas. Presumably the data from context.getImageData() is already an optimised array type…?

EDIT: I do not require the RGB value in the format “255,255,255” necessarily, it’s just all I could come up with for use as the array key. A different approach is also welcome 😀

  • 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-06T05:12:05+00:00Added an answer on June 6, 2026 at 5:12 am

    I don’t know if this is going to make any significant difference (you’d have to have a lot of values to see any appreciable performance difference), but you could:

    • Create keys using fast bit shifting operations instead of slow string concatenation
    • Cut out a few steps in the assignment:

    So:

    for(var i = 0, l = length; i < l; i += 4) {
       var key = (data[i] << 16) + (data[i+1] << 8) + data[i];
       countObject[key] = (countObject[key] || 0) + 1;
    }
    

    EDIT: Since you mentioned getting the RGB value back from the key, here is how it’s done:

    /** 
     * From a color key (integer)  return an object 
     * with keys 'r', 'g' and 'b'
     */
    var colorFromKey = function(key) {
       return {
          'r': (key >> 16) & 0xFF,
          'g': (key >> 8) & 0xFF,
          'b': key & 0xFF
       };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone suggest a way to modify JSONParser's getQuestionJSONFromUrl() method so that it stores
Can anyone suggest the best way to display a Textblock (with a text such
Can anyone suggest the best way to offer a file for download within a
Can anyone please suggest a way to replace back-slash '\' with slash '/' in
Can any one suggest me the best way to write code for uploading a
Can anyone suggest way to trace the mobile tower signals in android. Tracing means
Can anyone suggest a way of stripping tab characters ( \ts ) from a
can anyone suggest me way to display all records in MySQL table within a
Can anyone suggest a way to get the MVC templates installed without running the
Can anyone suggest a way to accomplish the following in ASP.Net Routing (for MVC

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.