Concatenación 4
Prueba para analizar el rendimiento de concatenar realizando interpolacion de cadenas de ES6.
HTML
<h2>
Method 1. A concatenation per instruction
</h2>
<table>
<tr id="data">
<th>Results</th>
</tr>
</table>
<p>
<a href="https://jsfiddle.net/juglan/fyhsb0s2/">Mehotd 1</a> |
<a href="https://jsfiddle.net/juglan/o1w5zcy7/">Method 2</a> |
<a href="https://jsfiddle.net/juglan/hsuwzb3o/">Method 3</a>
</p>
CSS
* {
font-family: Tahoma, Arial, Verdana;
}
td,
th {
border: 1px solid #666;
padding: 4px 8px;
}
th {
background-color: #E0E0E0;
}
table {
border-collapse: collapse;
}
a {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
h2 {
font-size: 18px;
}
JavaScript
function concatenate() {
var text = '',
values = ['12', '23', '34', '45', '56'];
// Se mantiene por similitud con las otras funciones, para que compute en los tiempos.
for (j = 0; j < 5; ++j) {
}
text = `${values[0]}${values[1]}${values[2]}${values[3]}${values[4]}`
}
function addResult(value, row, last) {
var node = document.createElement(last ? 'th' : 'td'),
textnode = document.createTextNode(value);
node.appendChild(textnode);
row.appendChild(node);
}
function test() {
var roundTime = 0,
row = document.getElementById('data'),
lap, startTime, i, endTime, currentResult;
for (lap = 0; lap < 10; ++lap) {
startTime = new Date();
for (i = 0; i < 10000000; ++i) {
concatenate();
}
endTime = new Date();
currentResult = endTime.getTime() - startTime.getTime()
roundTime += currentResult;
addResult(currentResult, row, false);
}
addResult((roundTime / 10) + 'ms', row, true);
}
test();