jezen / is-thirteen

Check if a number is equal to 13.
5.53k stars 467 forks source link

too fast #740

Open tezlm opened 2 years ago

tezlm commented 2 years ago

it doesnt feel like its doing anything because its running too fast

thedemons commented 2 years ago

Can we have an update on this issue? It's really bothering me.

Almenon commented 1 year ago

You can fix it on the user-side. In classic is-13 fashion, let me present a horribly overcomplicated solution:

  1. Make sure your document runs in a secure context. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements. You can set the Cross-Origin-Opener-Policy and Cross-Origin-Embedder-Policy headers
  2. Create a sharedarraybuffer - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
  3. Create a int32array from the sharedarraybuffer - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int32Array
  4. Pass the value into Atomics.wait and set a timeout.
  5. Call is-13

Alternatively, calculating 9000 digits of pi takes ~1 second on my machine, so you could just calculate 9000 digits of pi in a loop of X times to wait for X seconds. Your mileage may vary, test it out at http://ajennings.net/pi.html

igoro00 commented 10 months ago

Alternatively, calculating 9000 digits of pi takes ~1 second on my machine, so you could just calculate 9000 digits of pi in a loop of X times to wait for X seconds. Your mileage may vary, test it out at http://ajennings.net/pi.html

"Your mileage may vary" - we can't have that! On each machine calculating 9000 digits of pi will take vastly different amounts of time which will the UX different for every user. Hell, it will be different between each runs(changing loads on a users machine)!

I suggest implementing a benchmarking function that would get run before every is-13 call. It would try to estimate how many digits of pi you need to calculate before the actual is-13 to get as close to the target time as possible.