jgm / djot

A light markup language
https://djot.net
MIT License
1.71k stars 43 forks source link

Playground: slow on Safari #282

Open rauschma opened 6 months ago

rauschma commented 6 months ago

Currently, typing is slowed down. Does the code parse and render after every keystroke?

If yes then you could wait until the user pauses typing – e.g., like this:

const input = document.getElementById('my-input');
let timeout = null;
input.addEventListener('keyup', () => {
  if (timeout !== null) {
    clearTimeout(timeout);
  }
  timeout = setTimeout(
    () => {
      timeout = null;
      console.log('Parse and render:', textInput.value);
    },
    1000); // wait 1 second
});
jgm commented 6 months ago

There's already a debounce function. The debounce time is determined dynamically as follows:

    var elapsedTime = endTime - startTime;
    debounceMs = elapsedTime * 8;

The factor could be increased, but it doesn't feel laggy to me.

rauschma commented 6 months ago

Ah, I should have checked other browsers:

jgm commented 6 months ago

Interesting. I use it with Safari on M1 mac and have no issues.

jgm commented 6 months ago

You can try editing the code locally and changing that 8 to a higher number?

jgm commented 6 months ago

Also try with a large file and make sure the debounce is kicking in. When I try it with 240K file, I see exactly the behavior you're requesting -- I type and then when I stop, it renders. That's because the debounce time is determined dynamically based on how long it takes to parse and render the document.