Skip to content
Lu Wang edited this page Dec 5, 2013 · 11 revisions

Note: This page is still WIP

Hypnotic

Hypnotic is a JavaScript library/mechanism, it introduces a synchronous sleep() which does not freeze your browser.

Check out this fun demo if you have not.

Seriously, what is it?

Hypnotic is an asynchronous JavaScript interpreter, which is evolved from Narcissus and streamline.js.

In short, Hypnotic interprets your script. It suspends itself when it needs call sleep(), such that the JavaScript engine is free to handle all other events. Your script continues when Hypnotic is resumed after a while — everything is transparent to your script.

In short, Hypnotic calls setTimout(resumeHypnotic, delay); when you call sleep(delay);.

Why would I use it?

For a long time JavaScript is lack of a synchronous sleep(), which forces JavaScript developers to think and program in an asychronous fashion — which may be painful especially when timing logic is involved.

Now those days are over! You can come back and think like a normal person again!

Is it worth to introduce so much overhead only for sleep() ?

Lots of people have asked this question. OK you guys got a good point, and allow me to explain.

Hypnotic introduces sleep() in JavaScript for those who really need it, but it does not encourage any unnecessary usage of it — that is why it is called Hypnotic!

In short, it depends on how much you really need sleep(). Hypnotic is designed to provide such a function instead of to encourage the usage of it.

Maybe most JavaScript developers don't need it at all (or at least not so much with so much overhead), for all these years JavaScript has proven itself without a sleep(). You might argue that JavaScript is not designed for sleep() at all. I'd agree with that. But that's not the end of the story, I need sleep() for some reasons.

Consider how you can implement some animation like this? This one has been ported from the source code in C with emscripten, the function napms() (same as sleep()) is used everywhere: nested loops and branches — OK, with some libraries/patterns you can rewrite everything without hurting the readability, and I did it (except for the for-loops), which was not that hard as the code was pretty straightforward, but how about projects with hundreds of files and thousands of functions? I tried a few methods with clang and streamline.js, which didn't work until I made Hypnotic. (But I do have another idea now with clang, will try it) — If anyone got a better idea, do please tell me, thank you very much! I'm eager for a better solution myself.

Are emscripten guys doing what JavaScript was designed for? I guess not. But those crazy people really inspired me. Maybe the design of JavaScript will be changed in years? Who knows, who had predicted Web Workers before? BTW, Brendan Eich (who got a son called JavaScript) answered "When will you add threads to JavaScript" in his blog.

So this project might be useful for guys who really really need sleep() in JavaScript for all kinds of weird reasons. If it's not for you, that's OK, how about just having fun and fooling others? And thank you for your attention.

End of story.

Performance

There are overheads introduced by Hypnotic, also the interpreting might confuse the JIT compiler. But on the other hand, functions defined outside Hypnotic can be accessed by functions inside! So you can try to move computation-intensive functions (which are not likely to call sleep()) outside.

Flexibility

Hypnotic can be easily extended. See the semaphore demo.

API

Run Hypnotic.execute(YOUR_SCRIPT [,CALLBACK_AFTER_EXECUTION]). WIP

JavaScript Compatibility

In theory there should not be any thing incompatible.

Status

Hypnotic is currently in an early stage, you can use it for fun, but do not rely on it in your products.

Limitations

  • Currently only Firefox is supported due to Narcissus.
  • Syntax errors may not be reported correctly so far, so please be careful while I'm trying to fix it.