Created
March 8, 2024 04:30
-
-
Save tiagofassoni/ad1ab36e6a7605ab2af108f52cf04df1 to your computer and use it in GitHub Desktop.
Parallel map vs Standard Map using Deno
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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