Guest User

Untitled

a guest
Jul 23rd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. const { performance } = require("perf_hooks"); // if you're using node js
  2.  
  3. const arr = [];
  4. for (i = 0; i < 10000000; ++i) {
  5. let random = Math.floor(Math.random() * (20 - 0 + 1)) + 0;
  6. arr.push(random);
  7. }
  8.  
  9. function timer(lap) {
  10. if (lap)
  11. console.log(lap, "in:", (performance.now() - timer.prev).toFixed(3) + "ms");
  12. timer.prev = performance.now();
  13. }
  14.  
  15. timer();
  16.  
  17. const arr1 = [];
  18. for (i = 0; i < arr.length; i++) {
  19. arr1.push(arr[i] * 2);
  20. }
  21. timer("For Loop");
  22.  
  23. const arr2 = [];
  24. arr.forEach(a => {
  25. arr2.push(a * 2);
  26. });
  27.  
  28. timer("For each");
  29.  
  30. const arr3 = arr.map(a => a * 2);
  31.  
  32. timer("Map");
Add Comment
Please, Sign In to add comment