DEV Community

ezzabuzaid
ezzabuzaid

Posted on • Updated on

JavaScript Dart Compute

Dart lang offers an optimistic compute function that can execute a code on a separate isolate(thread) to avoid blocking the main thread or to do heavy computation away from the main thread.

It will "Spawn an isolate, run callback on that isolate, passing it a message, and (eventually) returns the value returned by callback."

In contrast, JavaScript also have threads! or web workers that can
"run scripts in background threads without interfering with the user interface".

hence, we can do compute in JavaScript that will spawn worker, run callback on that worker, passing it a message, and (eventually) returns the value returned by callback."

Example

function factorial(number) {
  if (number <= 0) {
    return 1;
  } else {
    return (number * factorial(number - 1));
  }
};

await compute(factorial, 200);
Enter fullscreen mode Exit fullscreen mode

References
"https://lnkd.in/d5PVjji"
"https://lnkd.in/dqqdBHt"

Top comments (0)