Skip to content

Instantly share code, notes, and snippets.

@tiagofassoni
Created March 8, 2024 04:30
Show Gist options
  • Save tiagofassoni/ad1ab36e6a7605ab2af108f52cf04df1 to your computer and use it in GitHub Desktop.
Save tiagofassoni/ad1ab36e6a7605ab2af108f52cf04df1 to your computer and use it in GitHub Desktop.
Parallel map vs Standard Map using Deno
import Parallel from 'npm:paralleljs'
import _ from 'npm:lodash'
/**
* Delay for a number of milliseconds
*/
function sleep(delay) {
var start = new Date().getTime();
while (new Date().getTime() < start + delay);
}
let arr = _.times(navigator.hardwareConcurrency, _.constant(1000));
let p = new Parallel(arr, {maxWorkers: navigator.hardwareConcurrency, synchronous: false});
const t0 = performance.now()
// block of code that needs to be measured
p.map(sleep)
const t1 = performance.now()
console.log(`Time it takes to run the parallel map: ${t1 - t0} ms`)
const t3 = performance.now()
// block of code that needs to be measured
arr.map(sleep)
const t4 = performance.now()
console.log(`Time it takes to run the function: ${t4 - t3} ms`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment