3 ms·
> an emoji, in reality, looks like this: “\xF0\x9F\x98\x81” … well, that’s one encoding of the UTF-8 encoding of the Unicode code point. > This is because set
by chrismorgan 9y ago
> an emoji, in reality, looks like this: “\xF0\x9F\x98\x81”
… well, that’s one encoding of the UTF-8 encoding of the Unicode code point.
> This is because setInterval(_=>{ },99) executes the function every 99ms
This is categorically wrong. You can’t trust setInterval to heed your request precisely at all: it’s a request that browsers take as a minimum only. Most browsers will call your function after the number of milliseconds you requested plus up to 16ms more, but some might wait even more than that (I think Safari in power saving mode doubles its tick time, to operate at 30fps; and background tabs typically won’t fire more than once a second these days).
Try running this snippet in your dev tools; it logs the number of milliseconds between calls:
t=Date.now();setInterval(()=>console.log(-t+(t=Date.now())), 99)
On Firefox I’m getting mostly 108–111, with the odd one a little higher, and a couple of 99s after running it for a few minutes.
(Some trivia on similar techniques for this measurement: `+new Date` is rather slow, `Date.now()` is about 7× faster in at least Firefox and Chrome, and `performance.now()` gets you microsecond precision (it returns a floating-point number in milliseconds, tied to an unspecified epoch instead of real-world time), and is a little slower than Date.now().)
- maury91 9y agoHi Chris, thanks for the corrections, I know you cannot trust the browser, but for the sake of simplicity, and because the purpose of the intro was describing the code I found on the internet, I opt for a simple "this is the trick", doesn't always work, but this is the idea
- chrismorgan 9y agoThe problem is that that part is completely wrong and dangerously misleading.
- didymospl 9y agoBesides that 99 % 6 = 3, so if the browser fired the setInterval function precisely every 99 ms, only two emojis would be displayed.
- maury91 9y agoThanks for noticing, I will update the article 99 % 6 === 3 ((99 % 6) + 99) % 6 === 0 ((((99 % 6) + 99) % 6) + 99) % 6 === 3
- chrismorgan 9y agoUpdate: performance.now() will generally get you ~20µs precision now: https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/ https://blog.mozilla.org/security/2018/01/03/mitigations-lan... Also I neglected to mention other frame rates; 60fps is the most common, but you do get some 144fps devices, where you’ll be looking at about 7ms delays rather than 16ms.