var literal = (strings, values) => strings.reduce(
(result, text, i) => (result += (text + ((i === strings.length - 1) ? "" : values[i]))), ""
);
var innerText = "This is innerText";
var text = `TEXT ${innerText} TEXT`;
var text = literal`TEXT ${innerText} TEXT`;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Template Literal | |
Tagged Template Literal |
Test name | Executions per second |
---|---|
Template Literal | 44424168.0 Ops/sec |
Tagged Template Literal | 6661134.5 Ops/sec |
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:
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
literal
) and may be less readable than traditional template literals.Other Considerations
When deciding between these approaches, consider the following:
Library and Syntax
The provided benchmark code uses the following JavaScript features:
reduce()
(a built-in array method)${innerText}
) using backticksvar 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:
Template Literal
: Creating a template literal with double backticks and simple string interpolation.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:
Please note that these alternatives might have different performance characteristics, syntax, or feature sets compared to template literals.