Code Monkey home page Code Monkey logo

workworkjs's Introduction

WorkWorkJS

A library that speeds up computation-heavy tasks in JavaScript with multi-threading and Web Workers.

Demo

https://workworkjs.github.io/workworkjs

Using it

Example

var arr = [64, 70, 80];

WorkWork.map(arr, function (elem) {
    return fibonacci(elem);
})
.then(function (resultArr) {
    console.log(resultArr);
})

// logs the 64th, the 70th, and the 80th fibonacci numbers

Installing it

You can get the raw js file here: https://raw.githubusercontent.com/workworkjs/workworkjs/master/workwork.js

OR run

npm install workworkjs --save 

and configure your static routing to correctly look into the node_modules folder.

Just put it into your script tag like so:

<script src="workwork.js"></script> 
<!-- or -->
<script src="node_modules/workworkjs/workwork.js"></script>

When should I use this?

WorkWork is best suited for situations where heavy blocking computation would otherwise be necessary. It is not intended to replace the original Array.prototype map and filter but rather to supplement them when heavy computation should be performed on a separate thread.

Example

var arr = [1, 2, 3];

WorkWork.map(arr, function (elem) {
    return elem + 1;
})
.then(function (resultArr) {
    console.log(resultArr);
})

This is slower than if you just used

Array.prototype.map 

Methods

WorkWork.map(arr, fn)

Takes in an array as the first argument, and a function as the second that returns a modified elem. Works just like the native Array.prototype.map except WorkWork.map returns a promise that must be resolved.

Example

var arr = [50000, 56456, 125694];

WorkWork.map(arr, nthPrime)
.then(function (resultArr) {
    console.log(resultArr);
});

// logs a new array with the 50,000th, the 56456th, and the 125694th prime numbers

WorkWork.filter(arr, fn)

Takes in an array as the first argument, and a function as the second that returns a true or false. Works just like the native Array.prototype.filter except WorkWork.filter returns a promise that must be resolved.

Example

var arr = [50000, 56456, 125694];

WorkWork.map(arr, nthPrime)
.then(function (resultArr) {
    return WorkWork.filter(resultArr, function (elem) {
        return (elem % 2 === 0);
    });
})
.then(function (resultFilter) {
    console.log(resultFilter);
})

// logs a new array with only the even prime numbers from before

Can also be used in isolation

Example

var arr = [50000, 56456, 125694];

WorkWork.filter(arr, isPrime)
.then(function (resultArr) {
    console.log(resultArr);
})

// logs an empty array since none of the elements are prime

Notes

Our name: https://www.youtube.com/watch?v=2ccZBlLJQ24


More about Web Workers: http://ejohn.org/blog/web-workers/

More about Blobs and inline Workers: http://www.html5rocks.com/en/tutorials/workers/basics/#toc-inlineworkers

Intel's Efforts in River Trail: https://software.intel.com/en-us/articles/river-trail-bringing-parallel-javascript-to-the-web

Parallel.js https://adambom.github.io/parallel.js/

workworkjs's People

Contributors

coldshower avatar

Watchers

 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.