Skip to content

stdout.write performance improvements #617

@ahaoboy

Description

@ahaoboy

Here is a simple example to fill the terminal and move a space:

When running with tjs, the terminal will flicker, but nodejs will run smoothly. It is not clear whether it is a platform problem of Windows, or the implementation of console.clear is different from deno/node.

let width = 0
let height = 0
if (typeof tjs !== 'undefined') {
  width = tjs.stdout.width
  height = tjs.stdout.height
} else {
  width = process.stdout.columns
  height = process.stdout.rows
}
let log;
if (typeof tjs !== 'undefined') {
  const encoder = new TextEncoder()
  log = s => tjs.stdout.write(encoder.encode(s))
} else {
  log = s => process.stdout.write(s)
}
let c = 0
const size = width * height

const sleep = n => new Promise(r => setTimeout(r, n));

(async function () {
  while (c < size) {
    console.clear()
    const s = new Array(size).fill('x')
    s[c] = ' '
    log(s.join(''))
    c++
    await sleep(16)
  }
})()



deno/console/clear

class CSI {
  static kClear = "\x1b[1;1H";
  static kClearScreenDown = "\x1b[0J";
}

clear = () => {
  this.indentLevel = 0;
  this.#printFunc(CSI.kClear, 1);
  this.#printFunc(CSI.kClearScreenDown, 1);
};

I tried running snake game with txiki, and it had no functional issues except for the terminal flickering.
https://github.com/ahaoboy/r-tui-txiki/blob/main/src/snake.tsx

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions