Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String() vs template literal
(version: 0)
string concat vs template literals (10 vars)
Comparing performance of:
String() vs Template literal
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var test = 123
Tests:
String()
var result = String(test)
Template literal
var result = `${test}`
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
String()
Template literal
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 123 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String()
5114083.0 Ops/sec
Template literal
15422680.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases to understand what's being tested. **Benchmark Definition** The benchmark is defined as a comparison between two approaches: string concatenation using the `+` operator (traditional "String()") and template literals (`${}`). **Options Compared** Two options are compared: 1. **String()**: This is traditional JavaScript string concatenation using the `+` operator. It creates a new string by appending each value to the original string. 2. **Template Literal**: Template literals are a newer syntax introduced in ECMAScript 2015 (ES6). They allow you to embed expressions inside backticks (`) and use variables or values directly. **Pros and Cons of Each Approach** * **String()**: + Pros: widely supported, easy to read for developers familiar with traditional string concatenation. + Cons: creates a new string object on each concatenation, leading to performance overhead due to the creation and garbage collection of these objects. * **Template Literal**: + Pros: more efficient than `String()`, as it only creates one string object and avoids the overhead of concatenation. Template literals also support interpolation, allowing you to embed expressions directly into strings. + Cons: requires knowledge of template literals syntax, which can be unfamiliar to some developers. **Library Usage** There is no specific library used in this benchmark. Both `String()` and template literals are built-in JavaScript features. **Special JS Feature/Syntax** Template literals use a special syntax (`${}`) that allows embedding expressions inside the backticks. This syntax is specific to ES6 and was introduced as a replacement for traditional string concatenation with `+`. **Other Alternatives** In addition to `String()` and template literals, other alternatives could be: * **Array.prototype.join()**: This method can also be used for string concatenation, although it's less common than the two options tested in this benchmark. * **Template String (optional)**: Some JavaScript engines have optional support for a template string syntax that is similar to template literals. Keep in mind that these alternatives might not provide the same performance benefits as template literals and may require additional setup or configuration.
Related benchmarks:
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
number to string: template literal vs toString vs string literal concat vs string constructor(2)
Comments
Confirm delete:
Do you really want to delete benchmark?