Giter VIP home page Giter VIP logo

simple-multithread-js's Introduction

simple-multithread

Based on Web Workers supported by all modern browsers.

TypeScript-ready.

Example 1

Multithreading is simple

async function main() {
    const args = [ 10, 20 ];
    const result = await execAsync((a, b) => {
        return a + b;
    }, args);
    console.log(result); // 30
}

Example 2

// import function execAsync(func, args)

function testCase() {
	let total = 0;
	for(let i = 0; i < 99999999; ++i) {
		total += Math.random() * 2;
    }
	return total;
}

async function test1() {
	console.time('single thread');
	testCase();
	testCase();
	testCase();
	console.timeEnd('single thread');
}

async function test2() {
	console.time('multi thread');
	await Promise.all([
		execAsync(testCase, []),
		execAsync(testCase, []),
		execAsync(testCase, [])
	]);
	console.timeEnd('multi thread');
}

Results

single thread: 3907.119140625ms
multi thread: 2017.375732421875ms

API

async function execAsync<ReturnT>(thread: (...args: any[]) => ReturnT, args: any[]): Promise<ReturnT>;

Limitations

Only plain type values for arguments.
No access to variables in scope, only arguments.
No native functions or functions defined in other origin.

Working on

  • Callback support

PS

There is much complex library - threads.

simple-multithread-js's People

Contributors

morglod avatar

Watchers

 avatar  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.