JSFiddle - React, Tailwind, and code Playground

by LyndseyB

Babel + JSX

const Arrays = (() => {
	const letters = 'abcdefghijklmnopqrstuvwxyz';
  
  function randomNumber(max) {
  	return Math.floor(Math.random() * max);
  }

	function createIntArray(counter) {
  	let i = 0;
    let arr = [];
    while(i < counter) {
    	arr.push(Math.floor(Math.random() * 10000));
      i++;
    }
  	return arr;
  }
  
  function createStrArray(counter) {
  	let i = 0;
    let arr = [];
    while(i < counter) {
    	let randomSize = randomNumber(letters.length);
      let size = 0;
      let str = '';
      
      while(size < randomSize) {
        str+= letters[randomNumber(letters.length)];
        size++;
      }     
      
    	arr.push(str);
      i++;
    }
  	return arr;
  }

	return {
    getRandomArray(options) {    
      const defaults = {
        type: 'number',
        count: 1000
      };
      const settings = Object.assign(defaults, options);     
      
      switch(settings.type) {
        case 'number':  
        	return createIntArray(settings.count);	
        case 'string':
          return createStrArray(settings.count);	
        default: 
          return [];
       }  
  	}
  };
})();

const words = Arrays.getRandomArray({ type: 'string', count: 100000 });

function lengthI(length, arr) {
	const result = [];
	for(let i = 0; i<arr.length; i++) {
  	const word = arr[i];
    
    if(word.length === length) {
    	result.push(word);
    }
  }
  
  return result;
}

function lengthC(length, arr) {
	const result = [];
	for(let i = 0; i<arr.length; i++) {
  	const word = arr[i];
    
    if(word.length !== length) {
    	continue;    	
    }
    result.push(word);
  }
  
  return result;
}

console.time('forIf');
const result = lengthI(6, words);
console.timeEnd('forIf');

console.time('forCon');
const result2 = lengthI(6, words);
console.timeEnd('forCon');

//console.log(result);
//console.log(result2);