|
| 1 | +const posts = [ |
| 2 | + {id: 1, upVotes: 2}, |
| 3 | + {id: 2, upVotes: 18}, |
| 4 | + {id: 3, upVotes: 1}, |
| 5 | + {id: 4, upVotes: 30}, |
| 6 | + {id: 5, upVotes: 50} |
| 7 | + ]; |
| 8 | + |
| 9 | +let sum = 0; |
| 10 | +console.log('**************** Reduce performace check ***************') |
| 11 | + |
| 12 | +console.time('reduce'); |
| 13 | +sum = posts.reduce((s, i)=> s+=i.upVotes,0); |
| 14 | +console.timeEnd('reduce') |
| 15 | + |
| 16 | +sum = 0; |
| 17 | +console.time('for loop'); |
| 18 | +for(let i=0; i<posts.length; i++) { |
| 19 | + sum += posts[i].upVotes; |
| 20 | +} |
| 21 | +console.timeEnd('for loop'); |
| 22 | + |
| 23 | +sum = 0; |
| 24 | +console.time('for each'); |
| 25 | +posts.forEach(element => { |
| 26 | + sum += element.upVotes; |
| 27 | +}); |
| 28 | +console.timeEnd('for each'); |
| 29 | + |
| 30 | +console.log('**************** Map performace check ***************') |
| 31 | +let newArray = []; |
| 32 | + |
| 33 | +console.time('map'); |
| 34 | +newArray = posts.map(d => d.id+1); |
| 35 | +console.timeEnd('map') |
| 36 | + |
| 37 | +newArray = []; |
| 38 | +console.time('for loop'); |
| 39 | +for(let i=0; i<posts.length; i++) { |
| 40 | + newArray.push(posts[i].id+1) |
| 41 | +} |
| 42 | +console.timeEnd('for loop'); |
| 43 | + |
| 44 | +newArray = []; |
| 45 | +console.time('for each'); |
| 46 | +posts.forEach(element => { |
| 47 | + newArray.push(element.id+1) |
| 48 | +}); |
| 49 | +console.timeEnd('for each'); |
| 50 | + |
| 51 | +console.log('**************** Filter performace check ***************') |
| 52 | + |
| 53 | +newArray = []; |
| 54 | + |
| 55 | +console.time('filter'); |
| 56 | +newArray = posts.filter(d => d.upVotes >= 2); |
| 57 | +console.timeEnd('filter') |
| 58 | + |
| 59 | +newArray = []; |
| 60 | +console.time('for loop'); |
| 61 | +for(let i=0; i<posts.length; i++) { |
| 62 | + if(posts[i].upVotes >= 2) { |
| 63 | + newArray.push(posts[i]) |
| 64 | + } |
| 65 | +} |
| 66 | +console.timeEnd('for loop'); |
| 67 | + |
| 68 | +newArray = []; |
| 69 | +console.time('for each'); |
| 70 | +posts.forEach(element => { |
| 71 | + if(element.upVotes >= 2) { |
| 72 | + newArray.push(element) |
| 73 | + } |
| 74 | +}); |
| 75 | +console.timeEnd('for each'); |
| 76 | + |
| 77 | +console.log('**************** Find performace check ***************') |
| 78 | +let obj = {}; |
| 79 | +console.time('find'); |
| 80 | +obj = posts.find(p => p.id == 5); |
| 81 | +console.timeEnd('find'); |
| 82 | + |
| 83 | +console.time('for'); |
| 84 | +for(let i=0; i<posts.length; i++) { |
| 85 | + if(posts[i].id == 5) { |
| 86 | + obj = posts[i]; |
| 87 | + } |
| 88 | +} |
| 89 | +console.timeEnd('for'); |
| 90 | + |
| 91 | +console.time('for each'); |
| 92 | +posts.forEach(element => { |
| 93 | + if(element.id >= 2) { |
| 94 | + obj = element |
| 95 | + } |
| 96 | +}); |
| 97 | +console.timeEnd('for each'); |
| 98 | + |
| 99 | +console.log('**************** Wining List Of Performance***************') |
| 100 | +console.log('for > foreach > map/reduce/filter/find'); |
0 commit comments