Git Product home page Git Product logo

parry's Introduction

parry

Tags CI Status Dependencies License


โš ๏ธ This project is a working project. Expect breaking changes.


A simple way of running functions in their own Worker that works with deno.

Usage

The following function will run in it's own Worker, enabling threading and therefor not blocking the main event loop. This becomes very useful for parallelization of certain computations or just a way of running a function and not blocking the main event loop while it runs. It is required to use the --allow-read flag for it to work due to an external js file being loaded in as the worker. If you want the Deno object to be avalible in the worker the --unstable flag is required. Permissions inside the workers are inherited and imports need to be dynamic.

import { parry } from "https://deno.land/x/parry/mod.ts";

async function countSerial(limit: number): Promise<void> {
  for (let i = 0; i < limit; i++) {}
}

console.time("4x CountSerial");
await Promise.all([
  countSerial(1e9),
  countSerial(1e9),
  countSerial(1e9),
  countSerial(1e9),
]);
console.timeEnd("4x CountSerial");

const threads = [
  parry(countSerial),
  parry(countSerial),
  parry(countSerial),
  parry(countSerial),
];

console.time("4x CountParallel");
await Promise.all([
  threads[0](1e9),
  threads[1](1e9),
  threads[2](1e9),
  threads[3](1e9),
]);
console.timeEnd("4x CountParallel");

parry.close();

The parallelized version is as expected 4x faster than the serial version of the count function:

4x CountSerial: 1885ms
4x CountParallel: 509ms

parry's People

Contributors

eliassjogreen avatar qu4k avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

shevernitskiy

parry's Issues

suggestion pass worker code as blob

Did you investigate possibility to provide worker part as blob? Kinda:

new Worker(URL.createObjectURL(new Blob(workercode.toString()))

Potentially it could eliminate dependency on separate file for worker.

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.