All Projects → jiayihu → Sinergia

jiayihu / Sinergia

Licence: mit
⚒️ Cooperative expensive tasks at 60fps via ES6 generators

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Sinergia

async generator
Making it easy to write async iterators in Python 3.5
Stars: ✭ 87 (-46.63%)
Mutual labels:  generators
Async Javascript
Asynchronous Programming in JavaScript
Stars: ✭ 372 (+128.22%)
Mutual labels:  generators
Opendnd
This is the main collection of OpenDnD Tools with generators for persons, dynasties, cities, towns, and more
Stars: ✭ 114 (-30.06%)
Mutual labels:  generators
csp.js
📺 CSP for vanilla JavaScript
Stars: ✭ 45 (-72.39%)
Mutual labels:  generators
minifaker
A lightweight alternative to faker.js
Stars: ✭ 64 (-60.74%)
Mutual labels:  generators
Kona
a node.js service framework built on koa.js (generators)
Stars: ✭ 23 (-85.89%)
Mutual labels:  generators
whatsup
Reactive framework, simple, fast, easy to use!
Stars: ✭ 115 (-29.45%)
Mutual labels:  generators
Vue Concurrency
A library for encapsulating asynchronous operations and managing concurrency for Vue and Composition API.
Stars: ✭ 147 (-9.82%)
Mutual labels:  generators
Learn Generators
JavaScript ES(6|2015) generators workshopper. Learn in practice. 🤘
Stars: ✭ 257 (+57.67%)
Mutual labels:  generators
Octo
A fuzzing library in JavaScript. ✨
Stars: ✭ 96 (-41.1%)
Mutual labels:  generators
username-generation-guide
A definitive guide to generating usernames for OSINT purposes
Stars: ✭ 38 (-76.69%)
Mutual labels:  generators
numpythia
The interface between PYTHIA and NumPy
Stars: ✭ 33 (-79.75%)
Mutual labels:  generators
Collecterator
Generator based PHP Collections
Stars: ✭ 33 (-79.75%)
Mutual labels:  generators
use-saga-reducer
Use redux-saga without redux
Stars: ✭ 72 (-55.83%)
Mutual labels:  generators
Boring generators
Boring generators aims to make your development faster by delegating boring setups to us.
Stars: ✭ 125 (-23.31%)
Mutual labels:  generators
co-sh
Using ES6 Proxies & Generators to run shell commands
Stars: ✭ 24 (-85.28%)
Mutual labels:  generators
Malli
Data-Driven Schemas for Clojure/Script.
Stars: ✭ 667 (+309.2%)
Mutual labels:  generators
Algebraic Effects
Manage side-effects in your javascript application cleanly with algebraic effects
Stars: ✭ 162 (-0.61%)
Mutual labels:  generators
Asynquence
Asynchronous flow control (promises, generators, observables, CSP, etc)
Stars: ✭ 1,737 (+965.64%)
Mutual labels:  generators
Troschuetz.random
Fully managed library providing various random number generators and distributions.
Stars: ✭ 47 (-71.17%)
Mutual labels:  generators

Sinergia

npm travis

sinergia is a tiny (1KB gzipped) library to run cooperative expensive tasks without blocking the UI during the computations and keeping 60fps frame rate.

Demo

A live example is available at https://jiayihu.github.io/sinergia/, with an animated box which should remain smooth at 60fps.

There are 2 examples:

  1. Long running loop: Running an expensive function (with a 2mln iterations loop) with each item of an iterable

  2. Long merge sort: Running a common merge sort with an array of 100k items

It's possible to play with the demo locally cloning the repo and running:

cd demo # Go to demo folder
npm install # Or `yarn install`
npm start

Installation

npm install sinergia --save

Usage

The following examples use co to consume the generator functions.

In this example work runs a long loop for every item, but every 100000 iterations it interrupts and gives the control to sinergia, which will resume the execution of work when more suitable.

Execution tasks are by definition cooperative because they decide when to yield the control of the execution.

By using yield inside your work you can decide the priority of the execution. Yielding often will run the task smoothly chunk by chunk but it will complete in more time. On the other hand yielding fewer times it will complete the task sooner but it will block more the main thread. Yielding zero times is equal to running the task synchronously.

import co from 'co';
import { sinergia } from 'sinergia';

function* work() {
  const iterable = 'Absent gods.'.split('');
  let result = '';

  for (let item of iterable) {
    let x = 0;

    while (x < 2000000) {
      x = x + 1;

      // Tell sinergia when the task can be interrupted and resumed later
      if (x % 100000 === 0) yield result;
    }

    result += item; // Simple result of task
    console.log(`Result of iteration:`, result);
  }

  yield result; // Yield latest result
}

const execute = co(function* () {
  return yield* sinergia(work);
});
execute.then((result) => {
  // If the work wasn't interrupted
  if (result) console.log(`Result: ${result.value}`);
});

Abort execution

Since sinergia is just a generator, you can use the returned object to abort the execution using .return() method of generators.

The method will return { value: any } with the value of the result computed on the latest item before aborting.

import co from 'co';
import { sinergia } from 'sinergia';

function* work() {
  const iterable = 'Absent gods.'.split('');
  let result = '';

  for (let item of iterable) {
    let x = 0;

    while (x < 2000000) {
      x = x + 1;

      // Tell sinergia when the task can be interrupted and resumed later
      if (x % 100000 === 0) yield result;
    }

    result += item; // Simple result of task
    console.log(`Result of iteration:`, result);
  }

  yield result; // Yield latest result
}

let iterator;

const execute = co(function* () {
  iterator = sinergia(work);
  return yield* iterator;
});
execute.then((result) => {
  // If the work wasn't interrupted
  if (result) console.log(`Result: ${result.value}`);
});

window.setTimeout(() => {
  const result = iterator.return();
  console.log('Interrupted result', result.value);
}, 5000);

API

sinergia(work: GeneratorFunction): Generator

It runs asynchronously the work function in not blocking way. Returns the Generator object.

Browser support

sinergia requires polyfills for:

  1. Promise like es6-promise or core-js Promise. If you use babel-polyfill it's already included.

  2. requestAnimationFrame/cancelAnimationFrame. See this gist as example.

Credits

Ispiration comes largely from @LucaColonnello and @cef62.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].