Joseph Evans
Joseph Evans

Web Developer

Blogger

Entrepreneur

Mentor

Joseph Evans

Web Developer

Blogger

Entrepreneur

Mentor

Blog Post

Multi-Threading JavaScript

January 9, 2020 JavaScript
Multi-Threading JavaScript

I promise, I’ll keep this one quite short, I’d just like to take a moment to talk about how easy it is to achieve multi threading in JavaScript, without the need for an array of files, which is what I’d imagine a lot of people do or have done in the past. I’ve implemented a way in which it’s possible to implement web workers running from the same JS file, kinda.

Without any delay, here’s an example of what the code may look like:

This example is incredibly simple, yet effective, the code that allows you to achieve this can be found here. I wrote this code quite some time ago, if anything to be perfectly honest, it was so long ago for me now, looking back at it, I’m a bit embarrassed 😶. However, regardless of its current state, it’s simple, yet effective, I mean if I were to write it today, I’d probably have some management class around this, maybe utilise an observable pattern so that when the thread finishes, you can subscribe to it rather than using a callback function, etc.

If you’re unsure of how the underlying code that I’ve written works, I’d strongly suggest that you look more into web workers to start with, maybe look more into how the event loop works also. Afterall, the more you know, the better! 🙂 But apart from those two suggestions, there’s not much more to add than that, the rest is pretty straightforward & simple.

Taggs:

Leave a Reply

avatar
  Subscribe  
Notify of