Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const data = Array.from({ length: 100000000 }, (_, i) =>
- Math.floor(Math.random() * 1000000),
- );
- function process1(input) {
- return input
- .filter((n) => n % 2 === 0)
- .map((n) => n * 2)
- .reduce((a, b) => a + b, 0);
- }
- function process2(input) {
- let sum = 0;
- for (const n of input) {
- if (n % 2 !== 0) {
- continue;
- }
- const doubled = n * 2;
- sum += doubled;
- }
- return sum;
- }
- const start1 = process.hrtime();
- process1(data);
- const end1 = process.hrtime(start1);
- console.log(`process1: ${end1[0]}s ${end1[1] / 1e6}ms`);
- const start2 = process.hrtime();
- process2(data);
- const end2 = process.hrtime(start2);
- console.log(`process2: ${end2[0]}s ${end2[1] / 1e6}ms`);
Advertisement
Add Comment
Please, Sign In to add comment