Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
Why does this value return true at the end of the loop, and then false at the start of it?

Consider the following function which should draw an image to the canvas in chunks, cache those chunks, and then on the next loop check against those cached chunks to make sure we don't draw anything more then once. If the console.log confirms that the cached chunk array is never cleared, why is it that when the result of pixels.data == cachedImage[y][x] is printed, it always returns false, but after updating the cache it reports as true?

async function temp() {
   const DIVIDE = 8;
   for(let y = 0; y < DIVIDE; y++) {
       if(cachedImage[y] == undefined) {cachedImage[y] = []; console.log(y);}
       for(let x = 0; x < DIVIDE; x++) {
           let widthChunk = (drawBuffer.width)/DIVIDE;
           let heightChunk = (drawBuffer.height)/DIVIDE;
           let pixels = ctx.getImageData(x*widthChunk,y*heightChunk,widthChunk-1,heightChunk-1);
           let pixeldata = pixels.data;
           debugBox3.innerHTML = (cachedImage[y][x] == pixels.data); // always "false"
           if(pixels.data == cachedImage[y][x]) {
           } else {
               cachedImage[y][x] = pixels.data;
               ctxFinal.putImageData(pixels,x*widthChunk,y*heightChunk)
           }
           debugBox4.innerHTML = (cachedImage[y][x] == pixels.data); // always "true"
       }
   }
}
await temp();

(full file avaliable here)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Suppose the following code:

let object1 = { key: 'value' }

var object2 = { key: 'value' }

One would think that object1 == object2 is true, but its not. Why? Because object1 and object2 are merely references to objects - to independent objects that is.

However, if we extend the code:

let object1 = { key: 'value' }

var object2 = { key: 'value' }

object1 = object2

Now we have set the variable object1 equal to the reference to object2. We have not copied object2.

Therefore object1 == object2 is now true.

When you say cachedImage[y][x] = pixels.data; you actually make cachedImage[y][x] a reference to pixels.data. Only then is it equal to pixels.data (which is also just a reference).

This is somewhat familiar to pointers in c, see: https://www.sitepoint.com/how-javascript-references-work.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!