Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Native JS: string plus VS template string!
(version: 0)
find best solution for concatenate 4 strings
Comparing performance of:
using plus operator vs using concat function vs using template literals
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var name = "name"; var id = "id";
Tests:
using plus operator
let result; for (let i = 0; i < 80; ++i) { result += id + ": 1, " + name + ": someItem"; } console.log(result); return result.length;
using concat function
let result; for (let i = 0; i < 80; ++i) { result += "".concat(id, ": 1, ", name, ": someItem"); } console.log(result); return result.length;
using template literals
let result; for (let i = 0; i < 80; ++i) { result += `${id}: 1, ${name}: someItem`; } console.log(result); return result.length;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
using plus operator
using concat function
using template literals
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
using plus operator
41692.6 Ops/sec
using concat function
40907.7 Ops/sec
using template literals
41354.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark measures which approach is best for concatenating four strings in JavaScript. The script preparation code sets two variables, `name` and `id`, to string values. The HTML preparation code is empty, indicating that this benchmark is purely focused on JavaScript performance. **Options Compared** Three options are being compared: 1. **Using the plus operator (`+`)**: This approach concatenates strings using the `+` operator. 2. **Using the `concat()` function**: This approach uses the built-in `concat()` method to concatenate strings. 3. **Using template literals (`${}`)**: This approach uses a feature introduced in ECMAScript 2015 (ES6) called template literals. **Pros and Cons of Each Approach** 1. **Using the plus operator (`+`)**: * Pros: Simple, widely supported, and efficient for small concatenations. * Cons: Can lead to memory leaks if not used carefully, as it creates new strings in each iteration. 2. **Using the `concat()` function**: * Pros: Efficient for large concatenations, as it avoids creating new strings in each iteration. * Cons: Requires more code than using the plus operator, and may be slower due to method call overhead. 3. **Using template literals (`${}`)**: * Pros: Convenient, efficient, and readable, especially for larger concatenations. * Cons: May have compatibility issues with older browsers or versions of JavaScript. **Library Usage** The `concat()` function is a built-in JavaScript function that concatenates strings. It's not a separate library. **Special JS Feature/Syntax** Template literals (`${}`) were introduced in ECMAScript 2015 (ES6). They provide a concise way to embed expressions inside string literals. **Benchmark Results** The benchmark results show the performance of each approach: * Using template literals: The fastest, with an average of 81717.671875 executions per second. * Using the `concat()` function: Slower than template literals, but faster than using the plus operator. * Using the plus operator: The slowest. **Alternatives** If you're looking for alternative approaches to concatenating strings in JavaScript, consider: 1. **Using a string builder**: A custom class that accumulates strings and provides methods for manipulating them. 2. **Using an array or list**: Storing strings in an array or list and joining them later can be efficient, especially for larger concatenations. 3. **Using a library**: Libraries like jQuery or Underscore.js provide string manipulation utilities that might offer better performance than native JavaScript approaches. Keep in mind that the best approach depends on your specific use case, performance requirements, and personal preference.
Related benchmarks:
Native JS: concatenate string with + vs template literals vs String.concat - My test
Native JS: concatenate string with + vs template literals vs String.concat my
Native JS: concatenate string with + vs template literals vs String.concat_0
Native JS2: concatenate string with + vs template literals vs String.concat
Comments
Confirm delete:
Do you really want to delete benchmark?