How to initialize two dimensional array in worker thread script in node.js

39
August 01, 2019, at 9:50 PM

Iam writing a node.js application which calculates the leveshtein distance over multiple and long strings/texts. To improve the perfomance i setup some worker threads to share the workload. Worker initalization and data passing via workerData works just fine. Node.js version is v12.7.0 on Windows10 64-bit.

The problem is that for the levenshtein distance i need to initialize a matrix of size n+1 * m+1 where n and m are the lengths of the strings to compare. But when executing the code the threads get started but dont do anything over that. I pinned down the problem to the 2d array initalization which is not working. But i have no idea why.

I checked that the code works by trying it single threaded without any worker thread. No problem there. I also tried different 2d array initalizations but they all failed aswell. I tried to increase the memory since the matrix can get pretty big.

This is the levenshtein algo i use for the calculation. It gets called while looping over some nested maps provided via workerData.

function levenshteinAlgo(s, t) {
    if(s === undefined || t === undefined)
        return -1;
    if(typeof s !== "string" || typeof t !== "string") {
        return -1;
    }
    let m = s.length;
    let n = t.length;
    //Both Array initializations and filling with zeroes dont work
    //And the application just freezes without errors.
    //let d = Array(m+1).fill().map(() => Array(n+1).fill(0));
    let d = [];
    for(let i = 0; i < (m + 1); i++) {
        d[i] = [];//d.push([]);
        for(let j = 0; j < (n + 1); j++) {
            d[i][j] = 0;    
        }
    }
    for(let i = 0; i < m; i++) {
        d[i][0] = i;
    }
    for (let j = 0; j < n; j++) {
        d[0][j] = j;
    }
    for (let i = 1; i <= m; i++) {
        for (let j = 1; j <= n; j++) {
            let cost = 0;
            if(s[i] === t[j]) {
                cost = 0;
            } else {
                cost = 1;
            }
            d[i][j] = Math.min(d[i-1][j] + 1, //deletion
                Math.min(d[i][j-1] + 1, //insertion
                    d[i-1][j-1] + cost)); //substitution
        }
    }
    return d[m][n];
}

It should work since it does for single threaded execution in the normal node.js instance. At least i would expect an error message but i dont get any.

I just have to interrupt the programm. I further more cant really debug the application since no breakpoint is triggered in the workerthread.js file. I dont know how to setup Visual Studio Code for parallel programming with node.js. Iam pretty new to node.js in generall.

Thanks for any advice.

READ ALSO
javascript function to get all the days of a week

javascript function to get all the days of a week

I am trying to get an array of all days in a week like today is Aug 1st so i need to get an array of this week which will look like

27
Modifying DOM nodes via JS causes memory leaks

Modifying DOM nodes via JS causes memory leaks

I'm working on a web template on WordPress that replaces the content of a specific div each period of time using jQueryHowever after a few loops the page crashes and the performance monitor of Chrome shows that the number of DOM nodes has increased on each iteration...

32
How comparate attributes of multiple fields with array

How comparate attributes of multiple fields with array

I am trying to comparator all attribute of the inputs with array is coming with those attributes name are in lowercases in DOM those are not lowercases

20
Why won&#39;t my iron-ajax network calls terminate?

Why won't my iron-ajax network calls terminate?

I'm creating a web page that uses iron-ajax calls to receive data from an APIWhen I inspect the page, the network tab shows seemingly endless iron-request objects

45