GithubHelp home page GithubHelp logo

slow.js's Introduction

This is some serious syntactic magic.

This is all automatic, no need to learn anything new. No new syntax, no new operators, functions or such.

Everything sort of just works, or at least, I want to draw you into this project by making it sound as if it was some magical box where parentheses and curly brackets go in and out comes ponies on butterflies that flutter across the clear azure sky. In reality it's not that magical unicorn dust I'm trying to convince you that it is.

This project is somewhat similar to the js deferred, async.js, coffescript defer, and the other ones. However, there are a few differences.

This one is pretty much automatic. You theoretically just wrap a function like slow(function(){}) and magically the insides run async and non-blocking.

In practice, it's not nearly as awesome.

There are a few significant bugs in the runtime, especially in for loops. Also, in general, it's not possible for it work if you omit the curly brackets in a block.

It automagically magicalates every part of the loop, so you really have no choice which loop to optimize for. If you're trying to do a SHA1 hash of a 6 megabyte string using this, it's going to be very very slow.

Every operation takes at least 1ms on Google Chrome's developer builds. At that rate, with a conservative estimate, it should take around 1.7 hours to finish with a result. On Firefox, it would take roughly 10ms at least, so around 17 hours to calculate the sha1 hash of a 6MB string. Versus what? three seconds to do a md5sum on my 600mb Ubuntu torrent? WebWorkers FTW, you should use that instead. I can not stress enough how slow this is. I even freaking named the entire project "slow.js", if that's not enough of a deterrent, I don't know what is.

In fact, even though by design, it should work on all browsers, in practice, it doesn't. I haven't tested it in any browser other than firefox and chrome, so I have no clue. If someone would like to add support for browsers (shouldn't be too hard, even IE shouldn't be that hard).

Anyway, how does it work? Well, lets start with a simple infinite loop.

var a = 0; while(true){ console.log(a++); } alert('the world has ended');

Running it would usually lock your browser, but if you wrap it in the magical slow() tags, something magical happens.

slow(function(){ var a = 0; while(true){ console.log(a++); } alert('the world has ended'); })

It doesn't freeze! Everything happens awesomely, but slower and non-blockingly. And the world still doesn't end!

To, say animate a box:

<script> slow(function(){ for(var i = 0; i < 1000; i++){ document.getElementbyId('blah').style.left = i+'px'; } }) </script>

And you see it fly off, quickly, but still animated. Now, how do we slow it down (even more?)

<script> slow(function(){ for(var i = 0; i < 1000; i++){ document.getElementbyId('blah').style.left = i+'px'; slow.sleep(100); } }) </script>

Neato, there's a sleep function, as expected. What if I want to download something?

slow(function(){ var xhrobj = new XMLHttpRequest(); xhrobj.open('get','/',true); xhrobj.send(null); while(xhrobj.readyState != 4){}; console.log(xhrobj.responseText); })

Sure, it's not that awesome, but hey, it works. And though there's a slow.download function, it probably doesn't work. Because unlike the predecessor of this project, http://code.google.com/p/js-compiler/ (which is horrible for it's own reasons).

What about callbacks? Well, that's what slow.Next is for, but I don't know if it works.

Any advantages? No, it's just thoroughly really bad and hard to debug.

slow.js's People

Contributors

antimatter15 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.