GithubHelp home page GithubHelp logo

chillout's Introduction

chillout.js

README (日本語)

Reduce JavaScript CPU usage by asynchronous iteration.

Build Status

Provides asynchronous iteration functions that have a Promise based interface and it can execute with low CPU usage. Each iteration adds delay if the processing is heavy to maintain the CPU stability. Iterate without delay if processing is fast. Therefore, it will realize friendly processing for your machine. It can execute JavaScript without "Warning: Unresponsive Script" alert in the browser.

You can use it in any JavaScript environment (Browser, Electron, Node.js).

Installation

Available on npm as chillout.

$ npm install chillout --save

This can also be installed with Bower.

$ bower install chillout
var chillout = require('chillout');
chillout.forEach(...)

Object chillout will be defined in the global scope if running in the browser window.

Compatibility

The limiting factor for browser/node support is the use of Promise.
You can use es6-shim or other Promise polyfills.

Benchmarks

Benchmarks the ForStatement and chillout.repeat.

function heavyProcess() {
  for (var i = 0; i < 10000; i++) {
    for (var j = 0; j < 10000; j++) {
      var v = i*j;
    }
  }
}

ForStatement

var time = Date.now();
for (var i = 0; i < 500; i++) {
  heavyProcess();
}
var processingTime = Date.now() - time;
console.log(processingTime);

CPU usage without chillout

  • Processing time: 51049ms.
  • CPU total average: 31.10%

chillout.repeat

var time = Date.now();
chillout.repeat(500, function(i) {
  heavyProcess();
}).then(function() {
  var processingTime = Date.now() - time;
  console.log(processingTime);
});

CPU usage with chillout

  • Processing time: 59769ms.
  • CPU total average: 22.76%

Benchmark Result

CPU usage with chillout

  ForStatement chillout.repeat
Processing time 51049ms. 59769ms.
CPU total average 31.10% 22.76%

You can confirm that chillout.repeat is running on a more low CPU usage than ForStatement.

chillout.js can run JavaScript in a natural speed with low CPU usage, but processing speed will be a bit slow.

One of the most important thing of performance in JavaScript, that is not numeric speed, but is to execute without causing stress to the user experience.

(Benchmarks: Windows8.1 / Intel(R) Atom(TM) CPU Z3740 1.33GHz)

Run Benchmark

You can test benchmark with npm run benchmark.


Iteration Functions

forEach

Executes a provided function once per array or object element.
The iteration will break if the callback function returns false, or an error occurs.

  • chillout.forEach ( obj, callback [, context ] )
    @param {Array|Object} obj Target array or object.
    @param {Function} callback Function to execute for each element, taking three arguments:

    • value: The current element being processed in the array/object.
    • key: The key of the current element being processed in the array/object.
    • obj: The array/object that forEach is being applied to.

    @param {Object} [context] Value to use as this when executing callback.
    @return {Promise} Return new Promise.

Example of array iteration:

var sum = 0;
chillout.forEach([1, 2, 3], function(value, i) {
  sum += value;
}).then(function() {
  console.log(sum); // 6
});

Example of object iteration:

var result = '';
chillout.forEach({ a: 1, b: 2, c: 3 }, function(value, key) {
  result += key + value;
}).then(function() {
  console.log(result); // 'a1b2c3'
});

repeat

Executes a provided function the specified number times.
The iteration will break if the callback function returns false, or an error occurs.

  • chillout.repeat ( count, callback [, context ] )
    @param {number|Object} count The number of times or object for execute the function.
    Following parameters are available if specify object:

    • start: The number of start.
    • step: The number of step.
    • end: The number of end.

    @param {Function} callback Function to execute for each times, taking one argument:

    • i: The current number.

    @param {Object} [context] Value to use as this when executing callback.
    @return {Promise} Return new Promise.

Example of specify number:

chillout.repeat(5, function(i) {
  console.log(i);
}).then(function() {
  console.log('end');
});
// 0
// 1
// 2
// 3
// 4
// end

Example of specify object:

chillout.repeat({ start: 10, step: 2, end: 20 }, function(i) {
  console.log(i);
}).then(function() {
  console.log('end');
});
// 10
// 12
// 14
// 16
// 18
// end

till

Executes a provided function until the callback returns false, or an error occurs.

  • chillout.till ( callback [, context ] )
    @param {Function} callback The function that is executed for each iteration.
    @param {Object} [context] Value to use as this when executing callback.
    @return {Promise} Return new Promise.
var i = 0;
chillout.till(function() {
  console.log(i);
  i++;
  if (i === 5) {
    return false; // stop iteration
  }
}).then(function() {
  console.log('end');
});
// 0
// 1
// 2
// 3
// 4
// end

forOf

Iterates the iterable objects, similar to the for-of statement.
Executes a provided function once per element.
The iteration will break if the callback function returns false, or an error occurs.

  • chillout.forOf ( iterable, callback [, context ] )
    @param {Array|string|Object} iterable Target iterable objects.
    @param {Function} callback Function to execute for each element, taking one argument:

    • value: A value of a property on each iteration.

    @param {Object} [context] Value to use as this when executing callback.
    @return {Promise} Return new Promise.

Example of iterate array:

chillout.forOf([1, 2, 3], function(value) {
  console.log(value);
}).then(function() {
  console.log('end');
});
// 1
// 2
// 3
// end

Example of iterate string:

chillout.forOf('abc', function(value) {
  console.log(value);
}).then(function() {
  console.log('end');
});
// a
// b
// c
// end

Comparison Table

You can reduce the CPU load by changing your JavaScript iteration to the chillout iteration.

Examples:

JavaScript Statement chillout
[1, 2, 3].forEach(function(v, i) {}) chillout.forEach([1, 2, 3], function(v, i) {})
for (i = 0; i < 5; i++) {} chillout.repeat(5, function(i) {})
for (i = 10; i < 20; i += 2) {} chillout.repeat({ start: 10, step: 2, end: 20 }, function(i) {})
while (true) {} chillout.till(function() {})
while (cond()) {} chillout.till(function() {
  if (!cond()) return false;
})
for (value of [1, 2, 3]) {} chillout.forOf([1, 2, 3], function(value) {})

Contributing

I'm waiting for your pull requests and issues. Don't forget to execute npm test before requesting. Accepted only requests without errors.

License

MIT

chillout's People

Contributors

polygonplanet avatar

Watchers

James Cloos 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.