andywer / andywer/threads.js

workers getting slow on every call

Aperta
#409 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub
bug question
Lingua principale
TypeScript
Stelle
3.5k
Fork
173
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

I use 3 workers to concurrently loop over a large array and return their levenshtein distance. They were fast at the beginning. But get slow on every function call.

```js
//index.js
...
const app = express();
const { spawn, Thread, Worker } = require('threads');
const workers = [await spawn(new Worker("./work.js")), await spawn(new Worker("./work.js")), await spawn(new Worker("./work.js"))];

for (var i = 0; i < 3; i++) {
const bigarray = [];
for (var j = 0; j < 135000; j++) {
bigarray.push(Math.random().toString());
}
workers[i].init(bigarray);
}

app.post('/', (req, res) => {
const query = req.body.query || Math.random().toString();
const promises = [];
for (const x of workers) {
promises.push(x.result(query));
}
Promise.all(promises).then(p => res.send(JSON.stringify(p)));
});

```

```js

//work.js

const { expose } = require("threads/worker");
const db = {};
const leven = require('leven');

expose({
init(array) {
db.data = array;
return;
},
result(query) {
const results = db.data.map(x => leven(x, query));
return { bestMatch: Math.min.apply(null, results) };
}
})
```

The above API gets 2 calls in 1000ms. The console also warns for reaching max event listeners.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.