Similar to this question but instead of concatenation, using and array and join
. If I am constructing a larger string from many smaller strings, is it more efficient to use template literals or push the strings into an array and use join
?
Simple example:
const arrayJoin = [constOne, constTwo, constThree].join(' ');
const templateLiterals = `${constOne} ${constTwo} ${constThree}`
A more complicated example:
const stringValues = {
a: 'apple',
b: 'butter',
c: 'cat',
d: 'dog',
e: 'egg',
f: 'fly',
};
const finalString1 = Object.keys(stringValues).map((string, i) => {
return [string, stringValues[string]].join(': ');
}).join(' ');
const finalString2 = Object.keys(stringValues).reduce((result, string, i) => {
return `${result}${string}: ${stringValues[string]} `;
}, '');