Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You use three loops:</p> <pre><code>// READ THE BELOW BEFORE YOU RUN THIS var r, g, b, rarray, garray, barray; rarray = []; for (r = 0; r &lt; 256; ++r) { garray = []; rarray.push(garray); for (g = 0; g &lt; 256; ++g) { barray = []; garray.push(barray); for (b = 0; b &lt; 256; ++b) { barray.push({r: r, g: g, b: b}); } } } </code></pre> <p>That produces an array (<code>rarray</code>) of arrays (each <code>garray</code>), each of which in turn contains an array (each <code>barray</code>) of objects. Each object has an <code>r</code>, <code>g</code>, and <code>b</code> value.</p> <p>Note that <a href="https://stackoverflow.com/questions/6007377/generating-0-255-3-times-double-or-triple-loop/6007518#6007518">as selbie points out</a>, you'll end up with a total of <code>256 * 256 * 256 = 16,777,216</code> entries spread across the various arrays. You'll have <code>256 * 256 = 65,536</code> <code>barray</code>s and <code>256</code> <code>garray</code>s. In all, that's <code>1 + 256 + (256 * 256) + (256 * 256 * 256) = 16,843,009</code> objects. Since all of these objects (<a href="http://blog.niftysnippets.org/2011/01/myth-of-arrays.html" rel="nofollow noreferrer">including the arrays</a>) are actually key=value maps, each entry will have a key portion and a value portion, and so just the references to those will be four bytes each. Then there's the actual key data for the array entries (1-3 characters = 2-6 bytes <em>minimum</em>) and the key data for each of our 16M final objects (which have three keys each, <code>r</code>, <code>b</code>, and <code>g</code>). Plus some other overhead. The details will vary a <em>lot</em> by JavaScript environment, but we're probably talking <em>no less than</em> (waves hands) 32-64 bytes per entry, for a total in memory of ~538MB-1076MB. Now, that's not a lot by modern computer standards, but it's a lot for a web page. :-) I used Chrome to do it, and it took ~1GB (did it in about 4 seconds&nbsp;&mdash; gotta love Chrome). In contrast, after several minutes and >2GB of RAM use, I gave up on Firefox. I wouldn't go near this in IE with a barge-pole.</p> <p><a href="http://jsbin.com/imoyo4/2" rel="nofollow noreferrer">Live example</a> (using only <code>0..4</code> rather than <code>0..255</code>).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload