{"id":13393439,"url":"https://github.com/polygonplanet/chillout","last_synced_at":"2026-02-23T05:31:30.466Z","repository":{"id":3330821,"uuid":"49082312","full_name":"polygonplanet/chillout","owner":"polygonplanet","description":"Reduce CPU usage by non-blocking async loop and psychologically speed up in JavaScript","archived":false,"fork":false,"pushed_at":"2023-01-06T02:05:37.000Z","size":2305,"stargazers_count":595,"open_issues_count":15,"forks_count":19,"subscribers_count":11,"default_branch":"master","last_synced_at":"2026-01-24T09:45:17.247Z","etag":null,"topics":["acceleration","async","async-await","async-functions","cpu","cpu-load","cpu-usage","cpu-utilization","javascript","lightweight-javascript-library","non-blocking","optimization","performance","speedup","user-experience"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/polygonplanet.png","metadata":{"files":{"readme":"README-ja.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-05T17:46:14.000Z","updated_at":"2025-11-07T21:51:36.000Z","dependencies_parsed_at":"2023-01-13T12:26:27.817Z","dependency_job_id":null,"html_url":"https://github.com/polygonplanet/chillout","commit_stats":null,"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"purl":"pkg:github/polygonplanet/chillout","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polygonplanet%2Fchillout","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polygonplanet%2Fchillout/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polygonplanet%2Fchillout/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polygonplanet%2Fchillout/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/polygonplanet","download_url":"https://codeload.github.com/polygonplanet/chillout/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polygonplanet%2Fchillout/sbom","scorecard":{"id":740489,"data":{"date":"2025-08-11","repo":{"name":"github.com/polygonplanet/chillout","commit":"0591516f63ea77a429873e8ef8f57e5321dba90c"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":1.7,"checks":[{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Code-Review","score":0,"reason":"Found 0/8 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 23 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Vulnerabilities","score":0,"reason":"80 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: GHSA-968p-4wvh-cqc8","Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92","Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw","Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7","Warn: Project is vulnerable to: GHSA-v6h2-p8h4-qcjw","Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg","Warn: Project is vulnerable to: GHSA-x9w5-v3q2-3rhw","Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5","Warn: Project is vulnerable to: GHSA-wg6g-ppvx-927h","Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x","Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275","Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c","Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq","Warn: Project is vulnerable to: GHSA-r9p9-mrjm-926w","Warn: Project is vulnerable to: GHSA-434g-2637-qmqr","Warn: Project is vulnerable to: GHSA-49q7-c7j4-3p7m","Warn: Project is vulnerable to: GHSA-977x-g7h5-7qgw","Warn: Project is vulnerable to: GHSA-f7q4-pwc6-w24p","Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747","Warn: Project is vulnerable to: GHSA-vjh7-7g9h-fjfh","Warn: Project is vulnerable to: GHSA-j4f2-536g-r55m","Warn: Project is vulnerable to: GHSA-r7qp-cfhv-p84w","Warn: Project is vulnerable to: GHSA-74fj-2j2h-c42q","Warn: Project is vulnerable to: GHSA-pw2r-vq6v-hr8c","Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc","Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp","Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3","Warn: Project is vulnerable to: MAL-2023-462","Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6","Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37","Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h","Warn: Project is vulnerable to: GHSA-7x7c-qm48-pq9c","Warn: Project is vulnerable to: GHSA-rc3x-jf5g-xvc5","Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp","Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695","Warn: Project is vulnerable to: GHSA-fvqr-27wr-82fm","Warn: Project is vulnerable to: GHSA-4xc9-xhrj-v574","Warn: Project is vulnerable to: GHSA-x5rq-j2xg-h7qm","Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw","Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9","Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm","Warn: Project is vulnerable to: GHSA-82v2-mx6x-wq7q","Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv","Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3","Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m","Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h","Warn: Project is vulnerable to: GHSA-8hfj-j24r-96c4","Warn: Project is vulnerable to: GHSA-wc69-rhjr-hc9g","Warn: Project is vulnerable to: GHSA-qrpm-p2h7-hrv2","Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55","Warn: Project is vulnerable to: GHSA-6fx8-h7jm-663j","Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9","Warn: Project is vulnerable to: GHSA-h7cp-r72f-jxh6","Warn: Project is vulnerable to: GHSA-v62p-rq8g-8h59","Warn: Project is vulnerable to: GHSA-h2p3-h48h-9jj7","Warn: Project is vulnerable to: GHSA-hfq9-rfpv-j8r8","Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp","Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw","Warn: Project is vulnerable to: GHSA-4g88-fppr-53pp","Warn: Project is vulnerable to: GHSA-4jqc-8m5r-9rpr","Warn: Project is vulnerable to: GHSA-g4rg-993r-mgx7","Warn: Project is vulnerable to: GHSA-25hc-qcg6-38wj","Warn: Project is vulnerable to: GHSA-qm95-pgcg-qqfq","Warn: Project is vulnerable to: GHSA-cqmj-92xf-r6r9","Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9","Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw","Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc","Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh","Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p","Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36","Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc","Warn: Project is vulnerable to: GHSA-52f5-9888-hmc6","Warn: Project is vulnerable to: GHSA-394c-5j6w-4xmx","Warn: Project is vulnerable to: GHSA-78cj-fxph-m83p","Warn: Project is vulnerable to: GHSA-fhg7-m89q-25r3","Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7","Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693","Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q","Warn: Project is vulnerable to: GHSA-72mh-269x-7mh5","Warn: Project is vulnerable to: GHSA-h4j5-c7cj-74xg"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-22T17:17:06.240Z","repository_id":3330821,"created_at":"2025-08-22T17:17:06.240Z","updated_at":"2025-08-22T17:17:06.240Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29738083,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-23T04:51:08.365Z","status":"ssl_error","status_checked_at":"2026-02-23T04:49:15.865Z","response_time":90,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["acceleration","async","async-await","async-functions","cpu","cpu-load","cpu-usage","cpu-utilization","javascript","lightweight-javascript-library","non-blocking","optimization","performance","speedup","user-experience"],"created_at":"2024-07-30T17:00:52.776Z","updated_at":"2026-02-23T05:31:30.439Z","avatar_url":"https://github.com/polygonplanet.png","language":"JavaScript","readme":"chillout.js\n===========\n\n重いループのCPU負荷を軽減し、体感的・心理的に高速化するJavaScriptライブラリ。\n\n[![NPM Version](https://img.shields.io/npm/v/chillout.svg)](https://www.npmjs.com/package/chillout)\n[![Build Status](https://travis-ci.com/polygonplanet/chillout.svg?branch=master)](https://travis-ci.com/polygonplanet/chillout)\n[![Bundle Size (minified)](https://img.shields.io/github/size/polygonplanet/chillout/dist/chillout.min.js.svg)](https://github.com/polygonplanet/chillout/blob/master/dist/chillout.min.js)\n[![GitHub License](https://img.shields.io/github/license/polygonplanet/chillout.svg)](https://github.com/polygonplanet/chillout/blob/master/LICENSE)\n\n## Table of contents\n\n* [概要](#概要)\n* [インストール](#インストール)\n* [動作環境](#動作環境)\n* [async / await](#async--await)\n* [ベンチマーク](#ベンチマーク)\n* [API](#api)\n  + [forEach](#foreach)\n  + [repeat](#repeat)\n  + [until](#until)\n  + [waitUntil](#waituntil)\n  + [forOf](#forof)\n* [比較表](#比較表)\n* [Pull Request](#pull-request)\n* [ライセンス](#ライセンス)\n\n## 概要\n\nchillout.js は「処理時間を短くする」という物理的な高速化とは違い、CPU負荷を軽減しリソースに余裕を持たせ、重い処理でも軽く感じさせることでユーザーにとって体感的・心理的な高速化につなげる JavaScript ライブラリです。\n\n### 重い処理から開放されるために\n\n重いWebページやゲームでは画面が読み込み中のままカクカクだったり、Webページに限らずnode.jsでバッチ処理するときでも高CPU負荷が続くとマシンごと重くなってしまいます。  \n\n特にスペックの低い端末では、CPU使用率100%の状態が続くと加熱されて冷却ファンが激しく回り、そのまま使い続けると冷却が追いつかずに熱暴走してしまう可能性もあります。\n\n### JavaScriptでCPU負荷を軽減するには？\n\n重い処理のほとんどはループ処理によって発生します。ループの中でさらにループ、その中でさらにループ…。単純に考えた場合、そうならないようループの途中で一定時間処理を休止させればいいんですが、それができません。  \nJavaScript には一定の時間休む sleep のような機能がないからです。そこで、sleepするにはどうするか？というと「非同期」でループ処理します。  \n\nJavaScript では、`setTimeout` や `process.nextTick` を使って同期処理を非同期化できます。\nそれと `Promise` を組み合わせると sleep のように一定時間CPUを休ませる非同期処理が実現できます。\n\n### 処理時間を短くするんじゃなく、体感速度を向上させる体感的・心理的な高速化\n\n処理の高速化というと、とにかく1ミリ秒でも処理時間を短くすることが手法とされますが、Webページやアプリ、ゲームといった、人が画面を見たり操作する場合 心理的に「速い」と感じればユーザーのストレスが減り結果として高速化につながります。  \n\nchillout.js は、ループ処理が重いときにはCPUが休まるくらいの休止時間、処理が速いときには休止時間なしか、わずかな休止時間をいれ本来のループを邪魔しないようにし、結果としてカクカクするような重さを感じさせずにループを実行します。  \n\nほとんどの場合、既存の JavaScript ループの代わりに chillout.js のループを使うことで CPU 使用率を減らすことができます。非同期ループとして扱う必要がありますが `async/await` を使うと扱いやすくなります。\n\nまた、処理が重くなるとでる **「警告: 応答のないスクリプト」** といったブラウザ警告なしでJavaScriptを実行できます。  \n\n\nブラウザ上、Electron、Node.js などの環境で使えます。\n\n\n## インストール\n\n### npm\n\n```bash\n$ npm install chillout\n```\n\n### CDN\n\n[jsdelivr.com](https://www.jsdelivr.com/package/npm/chillout) または [cdnjs.com](https://cdnjs.com/libraries/chillout) で CDN が利用できます。\n\n### 使い方\n\n`require` で使う場合の例:\n\n```javascript\nvar chillout = require('chillout');\n\nchillout.forEach([1, 2, 3], function(value) {\n  console.log(value);\n}).then(function() {\n  console.log('done');\n});\n\n// 1\n// 2\n// 3\n// 'done'\n```\n\nブラウザで実行してる場合は、**chillout** というオブジェクトがグローバル ( `window.chillout` ) に定義されます。\n\n## 動作環境\n\n`Promise` が動けば使えます(最新のブラウザはどれも動きます)。\n\n\n(古い環境で `Promise` がサポートされてない環境の場合は、[es6-shim](https://github.com/paulmillr/es6-shim) や、他の `Promise` polyfill を使ってください。)\n\n## async / await\n\n`async/await` が使える環境ならより簡潔に書けます。 chillout.js のAPIはすべて Promise を返すため `async/await` で扱えるようになっています。\n\n\n## ベンチマーク\n\nforループと `chillout.repeat` を比較します。\n\n```javascript\nfunction heavyProcess() {\n  var v;\n  for (var i = 0; i \u003c 5000; i++) {\n    for (var j = 0; j \u003c 5000; j++) {\n      v = i * j;\n    }\n  }\n  return v;\n}\n```\n\n### forループ\n\n```javascript\nvar time = Date.now();\nfor (var i = 0; i \u003c 1000; i++) {\n  heavyProcess();\n}\nvar processingTime = Date.now() - time;\nconsole.log(processingTime);\n```\n\n![CPU usage without chillout](https://raw.github.com/wiki/polygonplanet/chillout/images/benchmark-cpu-usage-without-chillout.png)\n\n* 処理時間: 107510ms.\n* CPU平均使用率(Nodeプロセス): **97.13%**\n\n### chillout.repeat\n\n\n```javascript\nvar time = Date.now();\nchillout.repeat(1000, function(i) {\n  heavyProcess();\n}).then(function() {\n  var processingTime = Date.now() - time;\n  console.log(processingTime);\n});\n```\n\n![CPU usage with chillout](https://raw.github.com/wiki/polygonplanet/chillout/images/benchmark-cpu-usage-using-chillout.png)\n\n* 処理時間: 138432ms.\n* CPU平均使用率(Nodeプロセス): **73.88%**\n\n### ベンチマーク結果\n\n![CPU usage with chillout](https://raw.github.com/wiki/polygonplanet/chillout/images/benchmark-cpu-usage-compare.png)\n\n| \u0026nbsp;                          | ForStatement (for文) | chillout.repeat |\n| ------------------------------- | --------------------:| ---------------:|\n| 処理時間                         |            107510ms. |       138432ms. |\n| CPU平均使用率(Nodeプロセス)         |           **97.13%** |      **73.88%** |\n\n\n`chillout.repeat` は forループ よりも低いCPU使用率で実行されているのが確認できます。  \nchillout.js は、より低いCPU使用率と自然な速さでJavaScriptを実行できますが、処理速度は少し遅くなります。  \n\nJavaScriptのパフォーマンスにおいて最も重要なことの一つは、数値的な速度ではなく安定したレスポンスによってユーザーにストレスを与えずに実行することです。これはブラウザ上で実行される場合、体感的な高速化の手段として特に重要です。\n\n*(ベンチマーク: chillout v3.1.2, Windows8.1 / Intel(R) Atom(TM) CPU Z3740 1.33GHz)*\n\n### ベンチマークを実行\n\n`npm run benchmark` でベンチマークを実行できます。\n\n----\n\n## API\n\n* [forEach](#foreach)\n* [repeat](#repeat)\n* [until](#until)\n* [waitUntil](#waituntil)\n* [forOf](#forof)\n\n### forEach\n\n与えられた関数 `callback` を、配列またはオブジェクトの各要素に対して一度ずつ実行します。  \n関数内で `chillout.StopIteration` を返すか、エラーが発生すると、それ以降のループ処理は実行されません。  \nこのメソッドは JavaScript の `Array forEach` のように使えます。\n\n* chillout.**forEach** ( obj, callback [, context ] )  \n  * @param {_array|object_} _obj_ 対象の配列またはオブジェクト。  \n  * @param {_function_} *callback* 各要素に対して実行するコールバック関数で、3つの引数をとります。  \n    - value: 現在処理されている配列の要素、またはオブジェクトの値。\n    - key: 現在処理されている配列の要素のインデックス、またはオブジェクトのキー。\n    - obj: `forEach` が適用されている配列またはオブジェクト。\n  * @param {_object_} [_context_] 任意。コールバック内で `this` として使用する値。  \n  * @return {_promise_} Promiseオブジェクトを返します。\n\n配列のループ例:\n\n```javascript\nvar values = ['a', 'b', 'c'];\n\nchillout.forEach(values, function(value, key, obj) {\n  console.log(value);\n}).then(function() {\n  console.log('done');\n});\n\n// 'a'\n// 'b'\n// 'c'\n// 'done'\n```\n\nオブジェクトのループ例:\n\n```javascript\nvar values = {\n  a: 1,\n  b: 2,\n  c: 3\n};\n\nchillout.forEach(values, function(value, key, obj) {\n  console.log(key + ':' + value);\n}).then(function() {\n  console.log('done');\n});\n\n// 'a:1'\n// 'b:2'\n// 'c:3'\n// 'done'\n```\n\n`async / await` を使ってループする例:\n\nこの例はファイルの内容をすべて出力し、最後に 'done' を出力します。\n\n```javascript\nasync function getFileContents(url) {\n  const response = await fetch(url);\n  return response.text();\n}\n\n// chillout.forEachのコールバックでasync functionを渡す\nasync function logFiles() {\n  const files = ['/file1.txt', '/file2.txt', '/file3.txt'];\n  await chillout.forEach(files, async url =\u003e {\n    const contents = await getFileContents(url);\n    console.log(contents);\n  });\n  console.log('done');\n}\n\nlogFiles();\n```\n\n### repeat\n\n与えられた関数 `callback` をを、引数で与えられた数だけ実行します。  \n関数内で `chillout.StopIteration` を返すか、エラーが発生すると、それ以降のループ処理は実行されません。  \nこのメソッドは JavaScript の `for` ステートメントのように使えます。\n\n* chillout.**repeat** ( count, callback [, context ] )  \n  * @param {_number|object_} _count_ 繰り返す回数またはオブジェクトで指定。  \n    オブジェクトで指定する場合は以下のキーが有効です。\n    - start: 開始する数。\n    - step: ステップ数。\n    - done: 終了する数。\n  * @param {_function_} _callback_ 各ループに対して実行するコールバック関数で、1つの引数をとります。\n    - i: 現在の数。\n  * @param {_object_} [_context_] 任意。コールバック内で `this` として使用する値。  \n  * @return {_promise_} Promiseオブジェクトを返します。\n\n回数を指定する例:\n\n```javascript\nchillout.repeat(5, function(i) {\n  console.log(i);\n}).then(function() {\n  console.log('done');\n});\n\n// 0\n// 1\n// 2\n// 3\n// 4\n// 'done'\n```\n\nオブジェクトで指定する例:\n\n```javascript\nchillout.repeat({ start: 10, step: 2, done: 20 }, function(i) {\n  console.log(i);\n}).then(function() {\n  console.log('done');\n});\n\n// 10\n// 12\n// 14\n// 16\n// 18\n// 'done'\n```\n\n`async / await` を使ってループする例:\n\nこの例は `/api/users/0` から `api/users/9` までのユーザーデータを出力し、最後に 'done' を出力します。\n\n\n```javascript\nasync function getUser(userId) {\n  const response = await fetch(`/api/users/${userId}`);\n  return response.json();\n}\n\n// chillout.repeatのコールバックでasync functionを渡す\nasync function logUsers() {\n  await chillout.repeat(10, async i =\u003e {\n    const user = await getUser(i);\n    console.log(user);\n  });\n  console.log('done');\n}\n\nlogUsers();\n```\n\n\n### until\n\n与えられた関数 `callback` を、 `chillout.StopIteration` が返されるかエラーが発生するまで繰り返します。  \nこのメソッドは JavaScript の `while (true) { ... }` ステートメントのように使えます。\n\n* chillout.**until** ( callback [, context ] )  \n  * @param {_function_} _callback_ 各ループに対して実行するコールバック関数。  \n  * @param {_object_} [_context_] 任意。コールバック内で `this` として使用する値。  \n  * @return {_promise_} Promiseオブジェクトを返します。\n\n```javascript\nvar i = 0;\nchillout.until(function() {\n  console.log(i);\n  i++;\n  if (i === 5) {\n    return chillout.StopIteration; // ループを止める\n  }\n}).then(function() {\n  console.log('done');\n});\n\n// 0\n// 1\n// 2\n// 3\n// 4\n// 'done'\n```\n\n`async / await` を使ってループする例:\n\nこの例はファイルの変更を監視して、変更された内容を出力します。\n\n```javascript\n// ミリ秒(msec)が過ぎるまで待機する関数\nfunction sleep(msec) {\n  return new Promise(resolve =\u003e setTimeout(resolve, msec));\n}\n\nasync function getFileContents(url) {\n  const response = await fetch(url);\n  return response.text();\n}\n\nlet previous = null;\n\n// chillout.untilのコールバックでasync functionを渡す\nasync function logNewFileContents() {\n  await chillout.until(async () =\u003e {\n    const contents = await getFileContents('./file1.txt');\n    if (previous === null) {\n      previous = contents;\n    }\n\n    if (contents !== previous) {\n      console.log('file changed!');\n      previous = contents;\n      return chillout.StopIteration; // ループを止める\n    }\n    await sleep(1000);\n  });\n  console.log(previous);\n}\n\nlogNewFileContents();\n```\n\n### waitUntil\n\n与えられた関数 `callback` を、 `chillout.StopIteration` が返されるかエラーが発生するまで繰り返します。  \nこのメソッドは JavaScript の `while (true) { ... }` ステートメントのように使え、 [`until`](#until) と同じ動作をしますが、CPU に負荷がかからないよう `until` よりゆっくり実行します。  \nこのメソッドは、何らかの処理が終わるまで待ちたいときに向いています。\n\n* chillout.**waitUntil** ( callback [, context ] )  \n  * @param {_function_} _callback_ 各ループに対して実行するコールバック関数。  \n  * @param {_object_} [_context_] 任意。コールバック内で `this` として使用する値。  \n  * @return {_promise_} Promiseオブジェクトを返します。\n\n```javascript\nchillout.waitUntil(function() {\n  // body要素が読み込まれるまで待機する\n  if (document.body) {\n    return chillout.StopIteration; // ループを止める\n  }\n}).then(function() {\n  document.body.innerHTML += 'body loaded';\n});\n```\n\n何らかの処理が終わるまで待つ例:\n\n```javascript\nsomeProcessing();\nchillout.waitUntil(function() {\n  if (isSomeProcessingDone) {\n    return chillout.StopIteration; // break loop\n  }\n}).then(function() {\n  nextProcessing();\n});\n```\n\n### forOf\n\n列挙可能なプロパティに対して、ループ処理を行います。\nこれは `for-of` ステートメントと同じループ処理をします。\n\n与えられた関数 `callback` を各ループに対して実行します。\n関数内で `chillout.StopIteration` を返すか、エラーが発生すると、それ以降のループは実行されません。\n\n* chillout.**forOf** ( iterable, callback [, context ] )  \n  * @param {_array|string|object_} _iterable_ 列挙可能なプロパティに対して、ループ処理を行うオブジェクト。  \n  * @param {_function_} _callback_ 各ループに対して実行するコールバック関数で、1つの引数をとります。\n    - value: 各ループ処理におけるプロパティの値。\n  * @param {_object_} [_context_] 任意。コールバック内で `this` として使用する値。  \n  * @return {_promise_} Promiseオブジェクトを返します。\n\n配列のループ例:\n\n```javascript\nchillout.forOf([1, 2, 3], function(value) {\n  console.log(value);\n}).then(function() {\n  console.log('done');\n});\n\n// 1\n// 2\n// 3\n// 'done'\n```\n\n文字列のループ例:\n\n```javascript\nchillout.forOf('abc', function(value) {\n  console.log(value);\n}).then(function() {\n  console.log('done');\n});\n\n// 'a'\n// 'b'\n// 'c'\n// 'done'\n```\n\n## 比較表\n\n既存のJavaScriptループを chillout.js のAPIに置き換えるとCPU負荷を軽減することができます。\n\n変換例:\n\n| JavaScript Statement                 | chillout                                                                      |\n| -------------------------------------|-------------------------------------------------------------------------------|\n| [1, 2, 3].forEach(function(v, i) {}) | chillout.forEach([1, 2, 3], function(v, i) {})                                |\n| for (i = 0; i \u003c 5; i++) {}           | chillout.repeat(5, function(i) {})                                            |\n| for (i = 10; i \u003c 20; i += 2) {}      | chillout.repeat({ start: 10, step: 2, done: 20 }, function(i) {})              |\n| while (true) {}                      | chillout.until(function() {})                                                  |\n| while (cond()) {\u003cbr\u003e\u0026nbsp;\u0026nbsp;doSomething();\u003cbr\u003e}                    | chillout.until(function() {\u003cbr\u003e\u0026nbsp;\u0026nbsp;if (!cond()) return chillout.StopIteration;\u003cbr\u003e\u0026nbsp;\u0026nbsp;doSomething();\u003cbr\u003e})    |\n| for (value of [1, 2, 3]) {}          | chillout.forOf([1, 2, 3], function(value) {})                                 |\n\n※ `async/await` が使える環境ではより簡潔に書けます。 chillout.js のAPIはすべて Promise を返すので `async/await` で扱えるようになっています。\n\n## Pull Request\n\nバグレポートや機能要望などの Issues や、Pull Request を歓迎しています。\nPull Requestの際は、 `npm run test` を実行してエラーがないことを確認していただけると助かります。\n\n## ライセンス\n\nMIT\n","funding_links":[],"categories":["JavaScript","javascript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpolygonplanet%2Fchillout","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpolygonplanet%2Fchillout","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpolygonplanet%2Fchillout/lists"}