Guest User

Untitled

a guest
Oct 10th, 2024
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const data = Array.from({ length: 100000000 }, (_, i) =>
  2.     Math.floor(Math.random() * 1000000),
  3. );
  4.  
  5. function process1(input) {
  6.     return input
  7.         .filter((n) => n % 2 === 0)
  8.         .map((n) => n * 2)
  9.         .reduce((a, b) => a + b, 0);
  10. }
  11.  
  12. function process2(input) {
  13.     let sum = 0;
  14.  
  15.     for (const n of input) {
  16.         if (n % 2 !== 0) {
  17.             continue;
  18.         }
  19.  
  20.         const doubled = n * 2;
  21.  
  22.         sum += doubled;
  23.     }
  24.  
  25.     return sum;
  26. }
  27.  
  28. const start1 = process.hrtime();
  29.  
  30. process1(data);
  31.  
  32. const end1 = process.hrtime(start1);
  33.  
  34. console.log(`process1: ${end1[0]}s ${end1[1] / 1e6}ms`);
  35.  
  36. const start2 = process.hrtime();
  37.  
  38. process2(data);
  39.  
  40. const end2 = process.hrtime(start2);
  41.  
  42. console.log(`process2: ${end2[0]}s ${end2[1] / 1e6}ms`);
  43.  
Advertisement
Add Comment
Please, Sign In to add comment