Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Template string vs join
(version: 1)
Comparing performance of:
By separator vs By template string
Created:
7 months ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const joinBySeparator = (a, b) => [a, b].join(':'); const joinByTemplateString = (a, b) => `${a}:${b}`;
Tests:
By separator
const result = joinBySeparator('Lorem', 'ipsum');
By template string
const result = joinByTemplateString('Lorem', 'ipsum');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
By separator
By template string
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Browser/OS:
Chrome 141 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
By separator
16063974.0 Ops/sec
By template string
179696672.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 7 months ago):
The benchmark described in the provided JSON compares two different ways of concatenating strings in JavaScript: using the `Array.prototype.join()` method with a separator and utilizing template literals (often referred to as template strings). Let's break down what is being tested and the implications of each approach. ### Options Being Compared 1. **Join by Separator**: This approach uses the `joinBySeparator` function which employs JavaScript's built-in `Array.prototype.join()` method. This method takes an array of strings and concatenates them into a single string, inserting a specified separator between each element. In the provided example, the separator is a colon (`:`). ```javascript const joinBySeparator = (a, b) => [a, b].join(':'); ``` 2. **Join by Template String**: This method uses JavaScript's template literals, which allow for easier string interpolation and variable inclusion. The `${}` syntax is used to embed expressions within a string. ```javascript const joinByTemplateString = (a, b) => `${a}:${b}`; ``` ### Performance Results From the benchmark results: - The test named **"By template string"** achieved an impressive **192,974,064 executions per second**. - In contrast, the **"By separator"** test reported **18,197,308 executions per second**. This indicates that the template string approach is significantly faster than the join method in this particular scenario. ### Pros and Cons #### Join by Separator **Pros:** - Familiar to developers who come from other programming languages where similar constructs exist. - Can be easily used to concatenate more than two strings by passing options into an array. **Cons:** - Performance is notably lower based on the benchmark, especially when dealing with a smaller number of strings. - Slightly more verbose than template literals for joining two strings. #### Join by Template String **Pros:** - Fast performance, as evidenced by the benchmark. - Syntax is clear and concise, making it easy to read and write. - Supports complex expressions and multi-line strings effectively. **Cons:** - Requires more modern JavaScript (ES6) environments. Older environments may not support template literals. - Template strings might be less intuitive for developers unfamiliar with the ES6 syntax. ### Other Considerations - **Modern JavaScript Compatibility**: Template literals are an ES6 (ECMAScript 2015) feature, so developers need to be aware of the compatibility on various project environments. Most modern browsers and Node.js versions support them, but legacy systems may not. - **Alternative String Concatenation Methods**: - **String Concatenation with `+`**: This is the traditional way to concatenate strings, and it performs well in many cases but becomes less readable with multiple variables. - **Array with `concat` Method**: Similar to `join`, but less commonly used for straightforward concatenation tasks. Overall, while string concatenation might seem trivial, understanding the performance implications and readability of different techniques is crucial for writing efficient and maintainable code.
Related benchmarks:
String template vs join
template
Template Literal Vs Array Join
Join vs interpolation
join vs template
template/join/yeahdude
Strings forming
Array join vs string template - Js
Array join vs string template v2
Comments
Confirm delete:
Do you really want to delete benchmark?