Created
January 10, 2019 23:10
-
-
Save CodeMan99/4c088e444ef303f9a8787451adee048e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
const RUN = 50000; | |
const FLAT_LENGTH = 9; | |
const a = [ | |
[0, 1, 2, 3], | |
4, | |
[5, 6, 7], | |
8 | |
]; | |
const perf = []; | |
const iter = () => { | |
for (let n = 0; n <= RUN; n++) { | |
const b = []; | |
for (const x of a) { | |
if (Array.isArray(x)) { | |
b.push(...x); | |
} else { | |
b.push(x); | |
} | |
} | |
if (b.length !== FLAT_LENGTH) { | |
throw new Error('Incorrect length: ' + b.length); | |
} | |
if (n === 0) { | |
if (b.some(x => Array.isArray(x))) { | |
throw new Error('Array "b" is not flat'); | |
} else { | |
console.time('iter'); | |
} | |
} | |
} | |
console.timeEnd('iter'); | |
}; | |
perf.push(iter); | |
const splice = () => { | |
for (let n = 0; n <= RUN; n++) { | |
const b = a.slice(); | |
for (let x, i = 0; i < b.length; i++) { | |
x = b[i]; | |
if (Array.isArray(x)) { | |
b.splice(i, 1, ...x); | |
// i--; // for multi-level nesting | |
i += x.length - 1; // for single-level nesting | |
} | |
} | |
if (b.length !== FLAT_LENGTH) { | |
throw new Error('Incorrect length: ' + b.length); | |
} | |
if (n === 0) { | |
if (b.some(x => Array.isArray(x))) { | |
throw new Error('Array "b" is not flat'); | |
} else { | |
console.time('splice'); | |
} | |
} | |
} | |
console.timeEnd('splice'); | |
}; | |
perf.push(splice); | |
const concatSpread = () => { | |
for (let n = 0; n <= RUN; n++) { | |
const b = [].concat(...a); | |
if (b.length !== FLAT_LENGTH) { | |
throw new Error('Incorrect length: ' + b.length); | |
} | |
if (n === 0) { | |
if (b.some(x => Array.isArray(x))) { | |
throw new Error('Array "b" is not flat'); | |
} else { | |
console.time('concat spread'); | |
} | |
} | |
} | |
console.timeEnd('concat spread'); | |
}; | |
perf.push(concatSpread); | |
const concatApply = () => { | |
const concat = Array.prototype.concat; | |
for (let n = 0; n <= RUN; n++) { | |
const b = concat.apply([], a); | |
if (b.length !== FLAT_LENGTH) { | |
throw new Error('Incorrect length: ' + b.length); | |
} | |
if (n === 0) { | |
if (b.some(x => Array.isArray(x))) { | |
throw new Error('Array "b" is not flat'); | |
} else { | |
console.time('concat apply'); | |
} | |
} | |
} | |
console.timeEnd('concat apply'); | |
}; | |
perf.push(concatApply); | |
if (Array.prototype.flat) { | |
const arrProtoFlat = () => { | |
for (let n = 0; n <= RUN; n++) { | |
const b = a.flat(1); | |
if (b.length !== FLAT_LENGTH) { | |
throw new Error('Incorrect length: ' + b.length); | |
} | |
if (n === 0) { | |
if (b.some(x => Array.isArray(x))) { | |
throw new Error('Array "b" is not flat'); | |
} else { | |
console.time('Array.prototype.flat'); | |
} | |
} | |
} | |
console.timeEnd('Array.prototype.flat'); | |
}; | |
perf.push(arrProtoFlat); | |
} | |
const run = function(i) { | |
try { | |
perf[i](); | |
} catch (e) { | |
console.error(e.message); | |
} | |
if (i + 1 < perf.length) { | |
setTimeout(run, 0, i + 1); | |
} | |
}; | |
perf.length && setTimeout(run, 0, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment