const thomas = (arr, len) => arr.length < len ?
thomas([...arr, ...arr], len) : arr.slice(0, len);
function karl(source, size) {
const iterations = Math.ceil(size / source.length);
return Array(iterations).fill(source).flat().slice(0, size);
}
const thomas2 = (arr, len) => [...new Array(len)]
.map((x, i) => arr[i % arr.length]);
function time(func, args, runNumberOfTimes) {
let start = performance.now();
for (let i = 0; i < runNumberOfTimes; i++) {
func(...args);
}
return Math.round(performance.now() - start) + ' ms';
}
let args;
console.log('Small array big repeat, run 2,000 times each');
args = [[1, 2, 3, 4, 5], 10000];
console.log('Array size: 5, make an array 10,000 items long.');
console.log('Karls solution', time(karl, args, 2000));
console.log('Thomas solution', time(thomas, args, 2000));
console.log('Thomas2 solution', time(thomas2, args, 2000));
console.log('\nBig array, smaller repeat, run 100 times each');
console.log('Array size: 10,000, make an array 100,000 items long');
args = ['x'.repeat(10000).split(''), 100000];
console.log('Karls solution', time(karl, args, 100));
console.log('Thomas solution', time(thomas, args, 100));
console.log('Thomas2 solution', time(thomas2, args, 100));