Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { performance } = require("perf_hooks"); // if you're using node js
- const arr = [];
- for (i = 0; i < 10000000; ++i) {
- let random = Math.floor(Math.random() * (20 - 0 + 1)) + 0;
- arr.push(random);
- }
- function timer(lap) {
- if (lap)
- console.log(lap, "in:", (performance.now() - timer.prev).toFixed(3) + "ms");
- timer.prev = performance.now();
- }
- timer();
- const arr1 = [];
- for (i = 0; i < arr.length; i++) {
- arr1.push(arr[i] * 2);
- }
- timer("For Loop");
- const arr2 = [];
- arr.forEach(a => {
- arr2.push(a * 2);
- });
- timer("For each");
- const arr3 = arr.map(a => a * 2);
- timer("Map");
Add Comment
Please, Sign In to add comment