Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String format vs template literal
(version: 0)
test speed, maybe also allocations
Comparing performance of:
Template literal vs String format
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
String.format = function() { return String._toFormattedString(false, arguments); }; String._toFormattedString = function(l, j) { var c = '', e = j[0]; for (var a = 0; true; ) { var f = e.indexOf('{', a), d = e.indexOf('}', a); if (f < 0 && d < 0) { c += e.slice(a); break; } if (d > 0 && (d < f || f < 0)) { c += e.slice(a, d + 1); a = d + 2; continue; } c += e.slice(a, f); a = f + 1; if (e.charAt(a) === '{') { c += '{'; a++; continue; } if (d < 0) break; var h = e.substring(a, d), g = h.indexOf(':'), k = parseInt(g < 0 ? h : h.substring(0, g), 10) + 1, i = g < 0 ? '' : h.substring(g + 1), b = j[k]; if (typeof b === 'undefined' || b === null) b = ''; if (b.toFormattedString) c += b.toFormattedString(i); else if (l && b.localeFormat) c += b.localeFormat(i); else if (b.format) c += b.format(i); else c += b.toString(); a = d + 1; } return c; }; var name = "name"; var id = "id";
Tests:
Template literal
for (let i = 0; i < 80; ++i) { let result = `${id}: 1, ${name}: someItem`; }
String format
for (let i = 0; i < 80; ++i) { let result = String.format("{0}: 1, {1}: someItem", id, name); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Template literal
String format
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):
**Benchmark Explanation** The provided JSON represents a JavaScript microbenchmark test case, specifically comparing the performance of template literals and string formatting in JavaScript. **Benchmark Definition** The benchmark definition consists of two scripts: 1. **Template Literal**: The script uses a template literal (`${id}: 1, ${name}: someItem`) to generate a formatted string. 2. **String Format**: The script uses the `String.format` function (customized) to format a string. **Options Compared** The benchmark compares two options: * **Template Literals**: Using template literals for string formatting * **String Formatting**: Using the customized `String.format` function **Pros and Cons** **Template Literals:** Pros: * Readability and maintainability are improved, as it allows for more human-readable syntax. * Less error-prone, as the syntax is standardized. Cons: * May be slower due to the creation of a new string object in the browser's parser (this has been mitigated in recent browsers). * Limited control over formatting options. **String Formatting:** Pros: * Provides fine-grained control over formatting options. * Can be faster, as it avoids creating a new string object. Cons: * Syntax can be less readable and maintainable than template literals. * Error-prone due to the complexity of the syntax. **Library and Purpose** The `String._toFormattedString` function is a custom implementation of string formatting. Its purpose is to emulate the behavior of built-in JavaScript string formatting functions, allowing for compatibility with older browsers or specific use cases. **Special JS Feature/Syntax** Template literals are a relatively recent feature introduced in ECMAScript 2015 (ES6). They provide a concise and readable way to embed expressions inside strings. The syntax is `stringLiteral template literal expression`. **Benchmark Result Interpretation** The benchmark result shows that the **Template Literal** approach is significantly faster than the **String Format** approach, with an execution rate of approximately 4987 executions per second compared to 23647 executions per second. Other Alternatives For string formatting in JavaScript, other alternatives include: * Using the `Intl` API for internationalized string formatting. * Utilizing a library like Moment.js for date and time formatting. * Employing a static string formatting utility, such as Handlebars or nunjucks.
Related benchmarks:
char index vs charAt() vs slice()
char index vs charAt() vs slice() vs substr()
char index vs charAt() vs slice() vs at()
Last char in a string: char index vs charAt() vs slice() vs at()
char index vs charAt() vs slice() vs charCodeAt()
Comments
Confirm delete:
Do you really want to delete benchmark?