Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Template String vs Template Function 4
(version: 0)
Comparing performance of:
Template String vs Template Function vs Template String Map vs Template String Map Inside
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var placeHolderElem = document.createElement('div'); placeHolderElem.id = "PlaceHolder"; document.body.append(placeHolderElem); var placeHolder = document.querySelector('#PlaceHolder'); var isEnabled = true; var itemList = [1, 2, 3, 4];
Tests:
Template String
const subTemplate = (item) => `<li>${item}</li>`; const template = `<div class="container"> ${isEnabled ? 'Enabled' : 'Disabled'} <ul>${(()=> { var output = ''; itemList.forEach((item) =>{ output += subTemplate(item) }); return output})()}</ul> </div>`; placeHolder.innerHTML = template;
Template Function
const templateFunction = function (obj) { obj || (obj = {}); var __t, __p = '', __j = Array.prototype.join; function print() { __p += __j.call(arguments, '') } with (obj) { __p += '<div class="container">\r\n ' + ((__t = (isEnabled ? 'Enabled' : 'Disabled')) == null ? '' : __t) + '\r\n <ul>\r\n '; itemList.forEach((item) => { ; __p += '\r\n <li>' + ((__t = (item)) == null ? '' : __t) + '</li>\r\n '; }); __p += '\r\n </ul>\r\n</div>'; } return __p; } placeHolder.innerHTML = templateFunction({ isEnabled });
Template String Map
const subTemplate = (item) => `<li>${item}</li>`; const template = `<div class="container"> ${isEnabled ? 'Enabled' : 'Disabled'} <ul>${itemList.map(subTemplate).join('')}</ul> </div>`; placeHolder.innerHTML = template;
Template String Map Inside
const template = `<div class="container"> ${isEnabled ? 'Enabled' : 'Disabled'} <ul>${itemList.map((item)=>`<li>${item}</li>`).join('')}</ul> </div>`; placeHolder.innerHTML = template;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Template String
Template Function
Template String Map
Template String Map Inside
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. **Benchmark Goal:** The goal of this benchmark is to compare the performance of three different approaches for generating HTML content using template literals (template strings) or template functions: 1. **Template String**: Using a template string with the `${}` syntax to embed expressions and values directly inside the string. 2. **Template Function**: Using a separate function (`templateFunction`) that takes an object as input and returns the rendered HTML string. 3. **Template String Map** (also known as Template Literal Method Call, TMCM): Using a template string with the `${}` syntax, but applying the `map` method to an array of items and joining them using `join('')`. 4. **Template String Map Inside**: Similar to TMCM, but with the map function applied directly inside the template string. **Options Compared:** The benchmark compares the performance of these four approaches: 1. Template String 2. Template Function 3. Template String Map (TMCM) 4. Template String Map Inside **Pros and Cons:** Here's a brief summary of the pros and cons of each approach: * **Template String**: Pros - simple, easy to read, and write. Cons - can be slower due to string concatenation. * **Template Function**: Pros - flexible, reusable, and potentially faster due to function call optimization. Cons - requires creating a separate function, which may add overhead. * **Template String Map (TMCM)**: Pros - similar performance to Template Function, but with the benefits of template strings. Cons - can be less readable for complex templates. * **Template String Map Inside**: Similar pros and cons as TMCM. **Library Usage:** There is no explicit library usage in this benchmark, but it's worth noting that modern JavaScript engines have optimized template literals to use a combination of string concatenation and function call optimization under the hood. However, the specific implementation details may vary between browsers. **Special JS Features/Syntax:** None of the approaches require any special JavaScript features or syntax beyond what's available in ECMAScript 2015 (ES6). **Alternatives:** Other alternatives for generating HTML content using template literals include: * Using `innerHTML` property to set the HTML content, which can be faster but also more prone to security issues. * Using a template engine library like Handlebars or Mustache, which provide additional features and optimization techniques. Overall, this benchmark provides a useful comparison of different approaches for generating HTML content using template literals, allowing developers to choose the best approach for their specific use case.
Related benchmarks:
TEMPLATE1
innerHTML vs insertAdjacentHTML template
Template String vs Template Function
Template String vs Template Function 3
Comments
Confirm delete:
Do you really want to delete benchmark?