Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Template Literal vs Tagged Template Literal
(version: 0)
Comparing performance of:
Template Literal vs Tagged Template Literal
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var literal = (strings, ...values) => strings.reduce( (result, text, i) => (result += (text + ((i === strings.length - 1) ? "" : values[i]))), "" ); var innerText = "This is innerText";
Tests:
Template Literal
var text = `TEXT ${innerText} TEXT`;
Tagged Template Literal
var text = literal`TEXT ${innerText} TEXT`;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Template Literal
Tagged Template Literal
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Template Literal
193001984.0 Ops/sec
Tagged Template Literal
17457676.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark is comparing two approaches to creating template literals in JavaScript: 1. **Template Literal**: This approach uses double backticks (``) to create a template literal, where the contents of the backticks are evaluated as JavaScript expressions. 2. **Tagged Template Literal**: This approach uses single backticks (`) followed by a string or symbol (the "tag") and then the template literal content. **Options Compared** The two options being compared are: * `Template Literal`: `var text = `TEXT ${innerText} TEXT`;" (double backticks with a simple string interpolation) * `Tagged Template Literal`: `var text = literal`TEXT ${innerText} TEXT`;" (`single backticks followed by a string/ symbol "tag") **Pros and Cons** 1. **Template Literal**: * Pros: Simple, widely supported, and easy to read. * Cons: May have performance overhead due to the need for explicit string concatenation or interpolation. 2. **Tagged Template Literal**: * Pros: Can provide more control over the template literal syntax, such as using a custom "tag" for specific use cases. * Cons: Requires additional setup (defining a function `literal`) and may be less readable than traditional template literals. **Other Considerations** When deciding between these approaches, consider the following: * **Readability**: Template literals are generally more readable due to their simplicity. * **Performance**: Tagged template literals can provide better performance if you need fine-grained control over the syntax. * **Customization**: If you want to create a custom template literal syntax or perform specific operations on the template literal content, tagged template literals might be a better choice. **Library and Syntax** The provided benchmark code uses the following JavaScript features: * `reduce()` (a built-in array method) * String interpolation (`${innerText}`) using backticks * Template literals (`var text = ...`) No additional libraries are used in this example. **Test Case Interpretation** Based on the provided test cases, it appears that the benchmark is comparing the performance of: 1. `Template Literal`: Creating a template literal with double backticks and simple string interpolation. 2. `Tagged Template Literal`: Creating a template literal with single backticks followed by a custom "tag" (the `literal` function) and string interpolation. The test results will likely show which approach has better performance, potentially revealing that `Template Literal` might have a small performance overhead due to the need for explicit string concatenation or interpolation. **Alternatives** If you're looking for alternatives to template literals, consider: * **String.format()`**: A built-in method for formatting strings in JavaScript (only available in ECMAScript 2020+). * **Internationalization API**: For more complex string formatting and localization tasks. * **Custom solution**: Implementing a custom string templating engine or library if the standard JavaScript features don't meet your needs. Please note that these alternatives might have different performance characteristics, syntax, or feature sets compared to template literals.
Related benchmarks:
String() vs template literal
number to string: template literal vs toString vs string literal concat vs string constructor
String() primitive vs template literal interpolation for representing a number as a string
Concatenation vs Template String
Comments
Confirm delete:
Do you really want to delete benchmark?