NoFreeze : a library that avoids freezing in javascript

The unresponsive warning happens a couple of time a year but every time, it’s frustrating. It prompts the message “A script on this page may be busy, or it may have stopped responding. You can stop the script now, or you can continue to see if the script will complete“.

I launched jskata not a long time ago and mostly talked about the undo feature. Now, I will talk about the jskata NoFreeze library to avoid freezing by splitting long processes into smaller ones.

You can look at the demo, the documentation or the source.

Old plain loop

This script loops the index variable from 0 to 1000000 and writes the current value in document.title. Unfortunately, it will freeze the page until the whole process is done.

  1. var index = 0; // index
  2. for(index = 0; index <= 1000000; index++) {
  3.     document.title = index;
  4. }

How to write a responsive for

I’m using jsKata.nofreeze library to avoid the freeze. This script will do the same as above. But, the page will remain responsive during the whole process.

  1. var index = 0; // index
  2. jsKata.nofreeze.forloop(
  3.     // the condition
  4.     function() { return index <= 1000000;  },
  5.     // the incrementor
  6.     function() { index++; },
  7.     // this is what will be executed
  8.     function fct() {
  9.         document.title = index;
  10.     }
  11. );

It uses closures to keep a resemblance to a good ol’ for loop.

Other loops : infinite and each

There are two other loops available. Infinite will loop indefinitely until you stop it. Each will loop through the properties of an object and loop in each one.

Take a look at the documentation to know more about them.

How to stop a process

As you see, every functions (forloop, infinite and each) return an object containing a stop function. So to stop a single process, just call it.

  1. var loop = jsKata.nofreeze.infinite(function() {//do nothing});
  2. loop.stop();

If you want to stop all the processes of a page, make this call :

  1. jsKata.nofreeze.stop();

More to come…

There’s much more you can do with that library but I wanted to keep it simple for this post. Next, I’ll talk about multi-process and other kinds of loop.

  • http://twitter.com/nfaugout Nicolas Faugout

    For this kind of need, why don't you use a simple setTimeout in the loop ?

    setTimeout( function() { document.title = index; }, 0);

    It will do the same loop but release the JS process between each iteration.

    The page will not freeze.

  • http://www.javascriptkata.com Dan Simard

    If you look at the source code ( http://j.mp/dtIwTi ), that's exactly what's behind it. But instead of writing it all, you have a simpler way of doing it by using the library. The examples in the page are trivial but you'd have to write tons of code by hand if you had more complex system.

  • Paul

    Why not just keep it simple and do this?

    jsKata.nofreeze.forloop = function (count, fn, sleep) {
    var i = 0, brk;
    sleep = sleep || 10;
    setTimeout(function() {
    brk = fn(i);
    i += 1;
    if(i < count || !brk) {
    setTimeout(arguments.callee, sleep);
    }
    }, sleep);
    };

  • http://www.javascriptkata.com Dan Simard

    I didn't want to break the for and I wanted to use all three parameters that a normal for uses. But I guess I could write a simple loop like the one you mentioned. It will be added shortly to the library.

  • http://www.javascriptkata.com Dan Simard

    I didn't want to break the for and I wanted to use all three parameters that a normal for uses. But I guess I could write a simple loop like the one you mentioned. It will be added shortly to the library.

  • Guest

    Who needs this, I dont understand… maybe everyone is not so aware of what is happening in their code…

  • profeval

    Amid the criticism, I think this is pretty neat. It will, however, add a TON of overhead to a loop that's expected to execute quickly. A larger/configurable chunk size would be neat as well.

    I think syntax such as this would be neat too

    function callback(index){
    ….document.title = index;
    }

    jsKata.nofreeze.simplefor( 0, 1000, callback );

  • http://www.javascriptkata.com Dan Simard

    Thanks for the comment! Everyone criticized the library has if I said “you all should be using it to write loops”. That would be quite stupid. You should use only when you have huge data to process and that freezes the browser. I wanted to keep the post simple to introduce how it worked and then write more about how to use it but people didn't try to go further before criticizing.

    This library surely adds overhead to a loop but you use it when you have to choose between overhead or freezing the browser. For me, the choice is obvious…

    I have a created a simpler loop : http://github.com/dsimard/jskata/blob/master/sr…

    You can call it like that :

    jsKata.nofreeze.forCount(1000, callback)

    It begins at 0 by default but if you want it to begin at 100, you write this :

    jsKata.nofreeze.forCount(1000, callback, {beginAt:100})

  • http://www.javascriptkata.com Dan Simard

    maybe you never had so much data to process that your browser never freezed.

  • tj

    Is there a way to have a callback function to notify when the loop is complete?

  • http://www.javascriptkata.com dsimard

    I'm looking at it… be back in a couple of minutes…

  • http://www.javascriptkata.com dsimard

    Look at the doc page : https://github.com/dsimard/jskata/wiki/jskata.nofreeze

    There's a “stopCallback” called each time a loop is stopped. Example of a forloop :

    var i = 0;
    jsk.nf.forloop(
    function() { return i < 10; },
    function() { i++; },
    function() { console.log(i); },
    null,
    function() { console.log(“Stopped at ” + i.toString()) } // <— HERE
    );

  • http://kalzdjsdcvi.com/ dYcxcgcvf

    2011…

    Great website. Plenty of useful information here. I am sending it to some friends ans also sharing in delicious. And obviously, thanks for your effort!…

  • http://kalzggdjsdcvi.com/ dYcxcgcvfgg

    2011…

    My brother recommended I might like this blog. He was entirely right. This post truly made my day. You can not imagine just how much time I had spent for this information! Thanks!…

  • http://Blzggzdjsdscvi.com/ dYcbbgx

    2011…

    I like the valuable information you provide in your articles. I’ll bookmark your weblog and check again here frequently. I am quite sure I’ll learn lots of new stuff right here! Good luck for the next!…

  • http://vilumparminavilumn.com/ Parminavilum

    Recommeneded websites…

    [...]Here are some of the sites we recommend for our visitors[...]……

  • http://Blzggzdjsdsccvi.com/ dYcbbgcx

    2011…

    Valuable information. Lucky me I found your web site by accident, and I am shocked why this accident did not happened earlier! I bookmarked it….