Pot.js Overview

This document is crazy english that is the one that most of sentences was translated literally and automatically into English from Japanese. Therefore, please acknowledge it though there is a possibility with improper sentences. You can refer the original Japanese document.

What's Pot.js?

Pot.js is an implemental utility library that can execute JavaScript without burdening the CPU.

for (var i = 0, len = items.length; i < len; i++) {
    var item = items[i];
    // Any heavy process.
}

Like this JavaScript loops that will be the CPU load and it may be warning the "unresponsive script".
Just replace to the Pot.js iterator simply.

Pot.Deferred.forEach(items, function(item, i) {
    // Any heavy process.
})

You can see how difference in the CPU usage as following table.

Iterator Data Size CPU Usage (MAX) CPU Usasge (AVG) Processing Time (ms)
for-statement 1MB 14.52% 10.61% 1211ms.
jQuery.each 1MB 16.28% 13.53% 1362ms.
Pot.Deferred.forEach 1MB 11.65% 5.53% 1647ms.
for-statement 5MB 24.85% 21.71% 4717ms.
jQuery.each 5MB 27.78% 24.29% 5570ms.
Pot.Deferred.forEach 5MB 13.74% 8.61% 8925ms.
for-statement 10MB 28.14% 25.92% 10699ms.
jQuery.each 10MB 31.09% 30.77% 15844ms.
Pot.Deferred.forEach 10MB 18.62% 9.77% 38909ms.

This result referenced from Compare CPU usage of while loop and Pot.Deferred.forEver and jQuery - Pot.js Blog (Japanese) (Core i5).
It is intended for JSON files of each size.
You can confirm that Pot.js iterator can executed without burdening the CPU from result.
Pot.js iterator can control speed and parallel processing if you need.
You can also run without warning "Unresponsive script".

Let execute loops with Pot.js in this page.

Download

Latest and Stable

Pot.js

PotLite.js (lite)

Repository

$ git clone git://github.com/polygonplanet/Pot.js

GitHub : polygonplanet/Pot.js - GitHub

Overview

Pot.js is a JavaScript library that can be performed without causing stress to the UI and the CPU load by using easy loop iteration functions.

With respect to load, you can implement a particular application without requiring the programmer to be aware of.

Pot.js is built around the asynchronous processing and iterator with Deferred.
Pot.Deferred is a Deferred object like MochiKit (or JSDeferred like).
That makes it possible to various iterations (forEach, filter, map, reduce, zip, repeat, some etc.).

Moreover, Pot.js is an utility library that handles string processes with various algorithms,
and it has the Signal object that can write like aspect-oriented (AOP),
and it has the Event object for DOM listener,
and treats it the File API for HTML5. etc.

And, Pot.js never pollute the prototype of the global objects.
We only define the 'Pot' object in the global scope basically.
You can use Pot.js with other available libraries without fear of conflict.

Pot.js is a cross-browser library that works on a Web browser, Node.js, userscript (Greasemonkey, Scriptish) XUL, or (Firefox Add-ons) etc.

PotLite.js is a light version that extracts only the part of asynchronous processing (Deferred etc.) has been implemented in Pot.js.

Install

Will work with common ways.

Example:

<script type="text/javascript" src="pot.min.js"></script>

<!--
  Or you can use the API hosting server for the Pot.js available.
  That referenced followings.
-->
<script type="text/javascript" src="http://api.polygonpla.net/js/pot/1.21/pot.min.js"></script>

Example for after loading of Pot.js:

<script type="text/javascript">
// You can use short method name in any by
//   Pot.globalize method. (see below).
Pot.globalize();

begin(function() {
    return request('./hoge.json', {
        mimeType : 'text/javascript'
    }).then(function(res) {
        return parseFromJSON(res.responseText);
    });
}).then(function(res) {
    return Deferred.forEach(res.items, function(value, key) {
        // ...
    });
});
// ...
</script>

For Node.js:

// Example to define Pot object on Node.js.
var Pot = require('./pot.min.js');
Pot.debug(Pot.VERSION);

Pot.Deferred.begin(function() {
    Pot.debug('Hello World!');
}).then(function() {...})
// ...

Example of Greasemonkey (userscript).

// ==UserScript==
// ...
// @require  https://github.com/polygonplanet/Pot.js/raw/master/pot.min.js
// ...
// ==/UserScript==

Pot.Deferred.begin(function() {
    return Pot.request('http://www.example.com/data.json', {
        mimeType : 'application/json'
    }).then(function(res) {
        return Pot.parseFromJSON(res.responseText);
    });
}).then(function(res) {
    Pot.debug(res);
    // do something...
});
//...

Pot.js API hosting server is available if you import directly from the Web with a limited version of the Pot.js.
On GitHub link above defects may occur due to differences in implementation because it is always up to date.

Example for use the Pot.js version 1.21:

This can be changed to fit the part of the released version 1.21.
When tagged version (e.g. 1.xx), we will put a new package to the API server together with the repository.

For example, if you use 1.21 with Greasemonkey (userscript):

// ==UserScript==
// ...
// @require  http://api.polygonpla.net/js/pot/1.21/pot.min.js
// ...
// ==/UserScript==

Note: This example uses the version numbers of 1.21, it is not always up to date.
Please select the version with more stable.

Examples of using PotLite.js:

You can use the API hosting server by change the link URL for PotLite.js like Pot.js's URL.

You can check the latest version's URL on Pot.js + PotLite.js - Hosting API Index.


As for jQuery plugin:

// Execute after loading with jQuery.
Pot.deferrizejQueryAjax();

// Ajax-based functions will return with Pot.Deferred object instance.
$.getJSON('/hoge.json').then(function(data) {
    alert(data.results[0].text);
}).rescue(function(err) {
    alert('Error! ' + err);
}).ensure(function() {
    return someNextProcess();
});

// Will added 'deferred' method for effects etc
//  that convert to Deferred functions.
$('div#hoge').deferred('hide', 'slow').then(function() {
    // (...Do something after hide() method completed.)
});

Pot.deferrizejQueryAjax() does not execute in library now. So, if you use as plugin, must call first.

Compatibility

Pot.js / PotLite.js works with the following web browsers.

  • Mozilla Firefox *
  • Internet Explorer 6+
  • Safari *
  • Opera *
  • Google Chrome *

And, it also designed for operate in following environments.

  • Greasemonkey (userscript)
  • Mozilla Firefox Add-On (on XUL)
  • Node.js
  • Other non-browser environment

Test Run

You can verify by running the operation test in the following page.

License

Dual licensed under the MIT and GPL v2 licenses.

Copyright © 2012 polygon planet

Contact

Author: polygon planet

polygon.planet.aqua[at]gmail.com

Thanks

Logo design:
yinyang302mg

Thanks for which became chance to make Pot.js / PotLite.js library:
JSDeferred, MochiKit (Mochi Media, Inc.), Tombloo developers.

JSDoc Document

The auto-generated documentation from the Pot.js / PotLite.js source code by Closure Compiler.

Refer more detailed implementation from the source code directly if needed.

Introduction

Pot.js / PotLite.js will define a "Pot" object in global scope for does non-conflict with other libraries.
If the environment is enabled the module (Node.js etc.), Pot.js / PotLite.js will use "exports" for "require". That case will not define an "Pot" object in global scope.

If you want to use short method name:

Pot.globalize();

Execute this code in the first. For example, you can be called only "begin", As method name of the "Pot.Deferred.begin".

And, "Pot.globalize" can be useful when define any object into the global scope.

var obj = {
    foo: function() { return 'foo' },
    bar: function() { return 'bar' }
};
Pot.globalize(obj);

alert(foo() + bar()); // 'foobar'

By providing such an argument on any object can be useful when you want to place any object in global scope.

Pot.js Blog

Pot.js official blog.

Twitter

Pot.js official Twitter account.

Blog Archives

Pot.js / PotLite.js - Blog Archives (Japanese).