Arrays
Array of n elements
Array(5).fill(0);
The above makes a sparse array, and sparse arrays are permanently slow.
Using Array()
has always been considered a bad practice and engines never bothered to optimizing it as they would have to check the prototype chain.
This is the more recommended way of creating an array of \$n\$ elements:
Array.from({ length: 5 }) => (_, i) => i;
Return array with conditional item
Suppose we want to return an array like this:
function getScripts() {
return [
"one.js",
"two.js",
"three.js",
];
}
But imagine based on some flag we want to skip "one.js" and return only the other two scripts. One approach is using some spread trickery:
function getScripts(b) {
return [
...(b ? ["one.js"] : []),
"two.js",
"three.js",
];
}
getScripts(0);
//=> ["two.js", "three.js"]
getScripts(1);
//=> ["one.js", "two.js", "three.js"]
But the iterator protocol is slower than some alternatives.
Doing the spread is idiomatic enough, but if performance becomes a problem, using [].concat()
is an alternative (and we want to avoid mutations like when using unshift()
push()
).
function getScripts(b) {
return [].concat(
b
? ["one.js"]
: [], "two.js", "three.jsx"
);
}
getScripts(0);
//=> ["two.js", "three.js"]
getScripts(1);
//=> ["one.js", "two.js", "three.js"]