Today 2020.12.11 I performperformed tests on MacOsmacOS HighSierra 10.13.6 on Chrome v87, Safari v13.1.2 and Firefox v83 for chosen solutions.
- solution O (based on
while) is the fastest (except Firefox for big N - but it's fast there) - solution T is fastest on Firefox for big N
- solutions M,P areis fast for small N
- solution V (lodash) is fast for big N
- solution W,X are slow for small N
- solution F is slow
function A(N) { return Array.from({length: N}, (_, i) => i + 1) } function B(N) { return Array(N).fill().map((_, i) => i+1); } function C(N) { return Array(N).join().split(',').map((_, i) => i+1 ); } function D(N) { return Array.from(Array(N), (_, i) => i+1) } function E(N) { return Array.from({ length: N }, (_, i) => i+1) } function F(N) { return Array.from({length:N}, Number.call, i => i + 1) } function G(N) { return (Array(N)+'').split(',').map((_,i)=> i+1) } function H(N) { return [ ...Array(N).keys() ].map( i => i+1); } function I(N) { return [...Array(N).keys()].map(x => x + 1); } function J(N) { return [...Array(N+1).keys()].slice(1) } function K(N) { return [...Array(N).keys()].map(x => ++x); } function L(N) { let arr; (arr=[ ...Array(N+1).keys() ]).shift(); return arr; } function M(N) { var arr = []; var i = 0; while (N--) arr.push(++i); return arr; } function N(N) { var a=[],b=N;while(b--)a[b]=b+1; return a; } function O(N) { var a=Array(N),b=0; while(b<N) a[b++]=b; return a; } function P(N) { var foo = []; for (var i = 1; i <= N; i++) foo.push(i); return foo; } function Q(N) { for(var a=[],b=N;b--;a[b]=b+1); return a; } function R(N) { for(var i,a=[i=0];i<N;a[i++]=i); return a; } function S(N) { let foo,x; for(foo=[x=N]; x; foo[x-1]=x--); return foo; } function T(N) { return new Uint8Array(N).map((item, i) => i + 1); } function U(N) { return '_'.repeat(5).split('').map((_, i) => i + 1); } function V(N) { return _.range(1, N+1); } function W(N) { return [...(function*(){let i=0;while(i<N)yield ++i})()] } function X(N) { function sequence(max, step = 1) { return { [Symbol.iterator]: function* () { for (let i = 1; i <= max; i += step) yield i } } } return [...sequence(N)]; } [A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X].forEach(f=> { console.log(`${f.name} ${f(5)}`); }) <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js" integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww==" crossorigin="anonymous"> </script> This shippetsnippet only presents functions used in performance tests - it does not perform tests itself!