Advertisement
kanatunta

array_filter_function

Dec 15th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const nums = [23, 45, 3, 2, 6, 7, 8, 8, 90, 87, 65, 4, 3, 2];//test data
  2. const line = () => {
  3.     console.log('-'.repeat(50))
  4. };//print pseudo line
  5. const times = 1000000;//repeating cycle
  6.  
  7. //========================================
  8. //high order function filter()
  9. console.time('high order function: filter()');
  10.  
  11. for (let indx = 0; indx < times; indx += 1) {
  12.     const filtered = nums.filter((a) => a >= 21);
  13. }
  14.  
  15. console.timeEnd('high order function: filter()');
  16.  
  17. line();
  18. //========================================
  19.  
  20. //naive approach
  21. console.time('naive approach');
  22. for (let indx = 0; indx < times; indx += 1) {
  23.     const filtered = [];
  24.     for (let e = 0; e < nums.length; e += 1) {
  25.         if (e >= 21) {
  26.             filtered.push(e);
  27.         }
  28.     }
  29. }
  30. console.timeEnd('naive approach')
  31. line();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement