The Computer Language
24.11 Benchmarks Game

mandelbrot Node.js #2 program

source code

/* The Computer Language Benchmarks Game
   https://salsa.debian.org/benchmarksgame-team/benchmarksgame/

   Greg Buchholz's simple C #2 program buffered, by Isaac Gouy. 
*/





var bit_num = 0, byte_acc = 0;
const iter = 50, limit = 2.0;
var zr, zi, cr, ci, tr, ti;

const h = +process.argv[2] || 200, w = h;
const bytesPerRow = w >> 3;

process.stdout.write(`P4\n${w} ${h}\n`);

for (let y = 0; y < h; ++y) {
  var row = new Uint8Array(bytesPerRow);
  var xbyte = 0;  

  for (let x = 0; x < w; ++x) {
    zr = zi = tr = ti = 0;
    cr = 2 * x / w - 1.5;
    ci = 2 * y / h - 1;
    for (let i = 0; i < iter && (tr + ti <= limit * limit); ++i) {
      zi = 2 * zr * zi + ci;
      zr = tr - ti + cr;
      tr = zr * zr;
      ti = zi * zi;
    }

    byte_acc <<= 1;
    if (tr + ti <= limit * limit) byte_acc |= 0x01;
    ++bit_num;

    if (bit_num === 8) {
      row[xbyte++] = byte_acc;
      byte_acc = 0;
      bit_num = 0;
    } else if (x === w - 1) {
      byte_acc <<= 8 - w % 8;      
      row[xbyte++] = byte_acc;
      byte_acc = 0;
      bit_num = 0;
    }
  }
  process.stdout.write(row);  
}

    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
v23.0.0


 Tue, 22 Oct 2024 23:28:39 GMT

MAKE:
cp -L mandelbrot.node-2.node mandelbrot.js

0.16s to complete and log all make actions

COMMAND LINE:
 /opt/src/node-v23.0.0/bin/node mandelbrot.js 16000

(BINARY) PROGRAM OUTPUT NOT SHOWN