Skip to content

Instantly share code, notes, and snippets.

@andrewodri
Created January 22, 2018 18:03
Show Gist options
  • Save andrewodri/2c351801cc14dab33d04280e13a25075 to your computer and use it in GitHub Desktop.
Save andrewodri/2c351801cc14dab33d04280e13a25075 to your computer and use it in GitHub Desktop.
String concatenation versus template literals
var precalculatedIso = (new Date()).toISOString();
console.time("es6Variable");
for(var i = 0; i < 100000; i++){
var test = `<body>
<article>
<time datetime='${ precalculatedIso }'>${ precalculatedIso }</time>
</article>
</body>`;
}
console.timeEnd("es6Variable");
console.time("stringVariable");
for(var i = 0; i < 100000; i++){
var test = "<body>"+
"<article>"+
"<time datetime='" + precalculatedIso +"'>"+ precalculatedIso +"</time>"+
"</article>"+
"</body>";
}
console.timeEnd("stringVariable");
console.time("es6Function");
for(var i = 0; i < 100000; i++){
var test = `<body>
<article>
<time datetime='${ (new Date()).toISOString() }'>${ (new Date()).toISOString() }</time>
</article>
</body>`;
}
console.timeEnd("es6Function");
console.time("stringFunction");
for(var i = 0; i < 100000; i++){
var test = "<body>"+
"<article>"+
"<time datetime='" + (new Date()).toISOString() +"'>"+ (new Date()).toISOString() +"</time>"+
"</article>"+
"</body>";
}
console.timeEnd("stringFunction");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment